shell-遍历所有txt,并且获取txt指定的内容
[lisa.ma@JUMPERBOX-OFFICE-02 ~]$ cat get_manager_ip.sh
#!/bin/bash
# Filename: get_manager_ip.sh
# Description: 脚本包括两个功能:
# 1,获取各个产品的manager的ip
# 2,check各个产品manager的进程是否存在
# 脚本参数使用看提示
# Author: lisa.ma
# Created: 2018-11-09
# Email: devops1066197187@163.com
project=$1
process=$2
path="/data/lisa.ma/scripts"
if [[ $# != 2 ]] ; then
echo "usage: $0 [igw|natgw|gulb...] [lsyncd] "
exit 1
fi
#pjct="ls ./scripts/*_$1.txt"
project="ls $path/*_$1.txt"
for f in `$project`;
do
isnull=`awk -v process=$2 '/manager/{print "ssh -t root@"$2 " \"pgrep -x " process "\"" }' $f|bash 2>/dev/null`
#红色的$2为全局变量,$2为内部变量
#echo $isnull
if [ -n "$isnull" ]
then
#echo "ok "
echo -e "\033[32m process is exist,ip address: \033[0m"
echo `awk '/manager/{print $2}' $f`
else
echo -e "\033[31mprocess not exist ,ip address: \033[0m"
echo `awk '/manager/{print $2}' $f`
fi
done
bash get_manager_ip gulb lsyncd
结果如下:
花絮1
cat ip | awk '{if(NR==4)F=1}{if(F){i++;if(i%4==1)print $0 "\n " }}'
cat ip |awk '{if(NR==4)F=1}{if(F){i++;if(i%4==1)print $0 }}'
cat ip | awk '{if(NR==4)F=1}{if(F){i++;if(i%4==1)print $0 }}'
ls ./scripts/*_$1.txt
ls ./scripts/*_gulb.txt
ls ./scripts/*_gulb.txt |awk -F "[/|_]" '{print $1}'
ls ./scripts/*_gulb.txt |awk -F "[/|_]" '{print $2}'
ls ./scripts/*_gulb.txt |awk -F "[/|_]" '{print $3}'
ls ./scripts/*_gulb.txt |awk -F "[/|_]" '{print $3 "_"$4}'
ls ./scripts/*_gulb.txt |awk '{print }'
ls ./scripts/*_gulb.txt |awk -F "[/|_|.]" '{print $3 "_"$4}'
ls ./scripts/*_gulb.txt |awk -F "[/|_|.]" '{print $4 "_"$5}'
ls scripts/pc03_gulb
ls scripts/pc03_gulb.txt
ls ./scripts/*_gulb.txt |awk -F "[/|_|.]" '{print $4 "_"$5}'
bash c gulb
cat gulb.text
pjct="ls *_$1.txt"
for f in `$pjct`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{print $1 " " a }'>> $1.text ;done
cd /data/lisa.ma/igw
for f in `ls *.txt`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{print $1 " " a }'>> igw.ip ;done
cd /data/lisa.ma/natgw
for f in `ls *.txt`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{print $1 " " a }'>> natgw.ip ;done
for f in `ls *.txt`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{printf $1"%s ",a }'>> natgw.ip ;done
for i in `ll *_natgw.txt`;do cat *_natgw.txt | grep manager | awk '{print $2}' > list.ip ;done;
scp root@IP:/data/CloudOps/scripts/*_natgw.txt ./natgw/
scp root@IP:/data/CloudOps/scripts/*_igw.txt ./igw/
scp root@IP:/data/CloudOps/scripts/*_gulb.txt ./gulb/
for i in `ll *_gulb.txt `;do cat "$i" | grep manager | awk '{print $2}' > gulb_list.ip ;done;
for i in `ls *.txt`;do awk '/manager/{print $2}' ./$i > gulb_list ;done;
#for f in `$pjct`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{sub("./scripts/","",$1);print $1 " " a }'>> $1.text ;done
#无法对齐地域后的IP地址,awk不支持print的缩进
#for f in `$pjct`;do ipa=`awk '/manager/{print $2}' $f` && [ -n "$ipa" ] && echo $f|awk -v a=$ipa -F "_" '{sub("./scripts/","",$1);printf "%-10s %s\n",$1,a }'>> $1.text ;done
#使用printf 同上面的功能一样。只是printf -10缩进,可以让IP对齐
花絮2:
ssh -t root@172.29.140.42 "pgrep -x lsyncd" 2>/dev/null
("ssh -t root@") $2 (1" \"pgrep -x (2" process "\"2) "1)
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print }'
1
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "root@ssh" }'
root@ssh
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "root@ssh" }'
root@ssh
[lisa.ma@jumpserver ~]$ "root@ssh
> ^C
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print ""root@ssh" }'
awk: {print ""root@ssh" }
awk: ^ invalid char '@' in expression
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print ""root@ssh"" }'
awk: {print ""root@ssh"" }
awk: ^ invalid char '@' in expression
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print ""root@ssh" }'
awk: {print ""root@ssh" }
awk: ^ invalid char '@' in expression
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh" }'
"root@ssh
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh"\" }'
awk: {print "\"root@ssh"\" }
awk: ^ backslash not last character on line
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh" }'
"root@ssh
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh" "1" }'
"root@ssh1
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh" """ }'
awk: {print "\"root@ssh" """ }
awk: ^ unterminated string
[lisa.ma@jumpserver ~]$ echo "1" |awk '{print "\"root@ssh" "\"" }'
"root@ssh"
ip addr show dev eth0 | grep -m 1 'inet ' | awk '{print $2}' | sed 's/\//\\\//'
ip a show dev eth0 |awk 'NR==3{sub("/24","",$2);print $2}'
花絮3
if [ "$a" == "23123" ];then echo "test" ;else echo "31231" ;fi
31231
if [ -n "$isnuil" ]; then echo ok ;else echo no ;fi;donev
if pgrep -x "$1" > /dev/null
then
echo "Running"
else
echo "Stopped"
fi
方式1:
if [[ test $( pgrep -f $1 | wc -l ) -eq 1 ]] && [[ test $(systemctl status lsyncd | grep Active | grep running | wc -l) -eq 1 ]]
then
echo "check ok "
else
echo "process is not exit"
fi
方式2:
#!/bin/sh
ps -fe|grep processString |grep -v grep
if [ $? -ne 0 ]
then
echo "start process....."
else
echo "runing....."
fi
cat test
hk
na01
hd02
cat test | awk '{printf "%s ",$0}'
for i in {hk,na01,hd02,sh2,ws,eu01,sa02,seoul,sg,sa03};do touch hypervisor-online.$i;done;
for i in {hk,na01,hd02,sh2,ws,eu01,sa02,seoul,sg,sa03};do echo "#$i" >> hypervisor-online.$i;done;
https://www.cnblogs.com/zdz8207/p/3282352.html
https://www.jb51.net/article/33217.htm
https://blog.csdn.net/shanzhizi/article/details/8123854
posted on 2018-11-12 10:49 keepimprove 阅读(814) 评论(0) 编辑 收藏 举报