常用的shell脚本
发布时间:2020-12-16 01:54:28 所属栏目:安全 来源:网络整理
导读:1、循环 # /bin/ bashi = 1 while [ 1 ]; do sleep 1 service xxx status if [$? == 0 ]; then echo "status" exit 1 fi service xxx start done #!/bin/ bashi = 1 for i in { 1 .. 5000 } do echo ------$i------ done 2、循环读取数组 configs= (a1,a2,a3
1、循环 # /bin/bash i=1 while [1]; do sleep 1 service xxx status if [$? == 0]; then echo "status" exit 1 fi service xxx start done #!/bin/bash i = 1 for i in {1..5000} do echo ------$i------ done 2、循环读取数组 configs=(a1,a2,a3,a4) for conf in ${configs[@]} do done 3、字符串对比 if [ "$service"x = "$service2"x ]; then fi 4、读取文件循环行 #!/bin/bash cat $1 | while read line do patch -p1 -i ../$line done 5、i自增 let i+=1 echo $i 6、判断相等和不相等 if [ $i -eq 10000]; then fi 7、统计行数 j=`cat /var/log/messages.log | grep "xxx" | wc -l` 8、统计c代码行数 find?.?-name?"*.c"|xargs?cat|grep?-v?-e?‘^$‘?-e?‘^s*//.*$‘?-e?‘^s*/*.*$‘?-e?‘^s**.*$‘?|?wc?-l 9、awk获取服务的进程ID pid=`service xx status | grep Main | awk ‘{print $3}‘` 10、当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次,如下命令实现杀死 某个关键字A的所有进程 ps -ef | grep A | grep -v ‘grep‘ | awk ‘{print $2}‘ | xargs -I % kill -9 % 11、批量scp文件 #!/bin/bash cat $1 | while read line do dir=${line%/*} mkdir -p $dir ./cp-passwd $line $dir done cp-passwd #!/bin/expect set file [lindex $argv 0] set dir [lindex $argv 1] spawn scp $file [email?protected]:$dir/ set timeout 30 expect "password:" send "[email?protected]!r" expect eof 12、获取rpm包名称,并创建文件目录,解压rpm源码包 #!/bin/bash dir=`pwd` src=centos_7.3_src rpm=centos_7.3_rpm cat $1 | while read src_rpm do rpm_name=${src_rpm%-*} rpm_name=${rpm_name%-*} echo $rpm_name cd $dir/$rpm if [ $? -ne 0 ];then echo ‘cd $dir/$rpm is failed‘ break fi mkdir $rpm_name && cd $rpm_name if [ $? -ne 0 ]; then echo ‘mkdir $rpm_name && cd $rpm_name is failed‘ break fi rpm2cpio $dir/$src/$src_rpm | cpio -id > /dev/null 2>&1 done 13、查看安装版本中是否已经包含所有rpm列表中的包 #!/bin/bash RPM= cat $1 | while read line do RPM=$(echo $line | awk -F "." ‘{print $1}‘) >/dev/null rpm -qa | grep $RPM > /dev/null if [ $? -ne 0 ]; then echo $line fi done 14、生成一个大文件 dd if=/dev/zero of=large_file_in bs=1K count=1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |