shell基础巩固第四天
for循环语句
for 变量 in 变量列表
do
指令
done
do
指令
done
or
for ((exp1;exp2;exp3))
do
指令
done
rename改名软件
[root@sf106232 test]# ll total 4 -rw-r--r-- 1 root root 68 Jun 20 17:41 del.sh -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_1.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_2.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_3.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_4.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_5.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_6.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 test_201706201_7.txt [root@sf106232 test]# rename "test" "suixiaofeng" *.txt [root@sf106232 test]# ll total 4 -rw-r--r-- 1 root root 68 Jun 20 17:41 del.sh -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_1.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_2.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_3.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_4.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_5.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_6.txt -rw-r--r-- 1 root root 0 Jun 20 15:49 suixiaofeng_201706201_7.txt [root@sf106232 test]#
先关闭3级别的开启的任务,然后开启需要开启的。
for abc in `chkconfig --list|grep 3:on |awk '{print $1}'`; do chkconfig --level 3 $abc off ;done [root@sf106232 test]# for abc in crond network rsyslog sshd sysstat ; do chkconfig --level 3 $abc on ; done [root@sf106232 test]# chkconfig --list|grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off rsyslog 0:off 1:off 2:off 3:on 4:off 5:off 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:off 2:on 3:on 4:off 5:on 6:off or chkconfig --list|grep 3:on |grep -vE "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig " $1 "off"}'
1到100的加法.
[root@sf106232 script]# sh 20170705.sh 5050 [root@sf106232 script]# cat 20170705.sh #!/bin/sh for ((i=1;i<=100;i++)) do ((sum=sum+i)) done echo $sum
数据库分库备份脚本开发使用。
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-06 13:52 # Filename: 20170706.sh # Description: #**************************************************** PATH="/usr/local/mysql/bin:$PATH" DBPATH=/back MYUSER=root MYPASS=test SOCKET=/db/mysql.sock MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYSUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET" mkdir $DBPATH/$(date +%F) [ ! -d "$DBPATH" ] && mkdir $DBPATH for dbname in `$MYCMD -e "show databases;" | sed '1,2d'| egrep -v "mysql|schema"` do $MYSUMP $dbname | gzip >$DBPATH/$(date +%F)/${dbname}_$(date +%F).sql.gz # $MYSUMP $dbname > $DBPATH/${dbname}_$(date +%F).sql done 备注: mysqldump -u$MYUSER -p$MYPASS -S /db/mysql.sock dbname |gzip > dbname.sql.gz 备份压缩
数据库分库分表备份脚本开发使用。
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-06 13:52 # Filename: 20170706.sh # Description: #**************************************************** PATH="/usr/local/mysql/bin:$PATH" DBPATH=/u02/back MYUSER=root MYPASS=test SOCKET=/tmp/mysql.sock MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYSUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET" [ ! -d "$DBPATH" ] && mkdir $DBPATH for dbname in `$MYCMD -e "show databases;" | sed '1,2d'| egrep -v "mysql|schema"` do mkdir $DBPATH/$(date +%F)/${dbname} -p for table in `$MYCMD -e "show tables from $dbname;"|sed '1d'` do $MYSUM $dbname $table |gzip >$DBPATH/$(date +%F)/${dbname}/${dbname}_${table}.sql.gz done done
生产环境下批量检查web服务是否正常,并发送邮件或者手机报警
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-06 15:01 # Filename: web_url.sh # Description: #**************************************************** path=/u02/script/script MAIL_GROUP="Daniel_gtj@163.com 258818040@qq.com" PAGER_GROUP="17621061569 18326885639" LOG_FILE="/tmp/web_check.log" [ ! -d "$path" ] && mkdir -p $path function Url_LIST () { cat >$path/domain.list<<EOF https://bk.devopstack.cn http://cool360.org http://www.cool360.org http://7ipoint.cool360.org EOF } function CheckURL () { FAILCOUNT=0 for ((i=1;i<=3;i++)) do wget -T 5 -q --tries=1 --spider $1 >/dev/null 2>&1 if [ $? -ne 0 ] then let FAILCOUNT+=1; else break fi done return $FAILCOUNT } function MAIL () { local SUBJECT_CONTENT=$1 for MAIL_USER in `echo $MAIL_GROUP` do mail -s "$SUBJECT_CONTENT" $MAIL_USER < $LOG_FILE done } function PAGER () { for PAGER_USER in `echo $PAGER_GROUP` do TITLE=$1 CONTACT=$PAGER_GROUP HTTPPGW=http: ##对应短信接口链接 ##send_message method curl -d cdkey= -d password= -d phone=$CONTACT -d message="$TITLE[$2]" $HTTPPGW done } function SendMSg() { if [ $1 -ge 3 ] then RETVAL=1 NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"` SUBJECT_CONTENT="http://$2 is error,${NOW_TIME}." echo -e "$SUBJECT_CONTENT" |tee $LOG_FILE MAIL $SUBJECT_CONTENT PAGER $SUBJECT_CONTENT $NOW_TIME else echo "$2 is ok" RETVAL=0 fi return $RETVAL } function main() { Url_LIST for url in `cat $path/domain.list` do CheckURL $url SendMSg $? $url done } main
在这篇文章中写了简单的web_url检测脚本:https://bk.devopstack.cn/archives/746.html
备注:这个脚本函数相互之间联系比较紧密,值得好好学习。
获取数字加字母的随机数
[root@sf106232 script]# echo $RANDOM|md5sum |cut -c 1-10
9b1a6dc629
[root@sf106232 script]# echo $RANDOM|md5sum |cut -c 1-10
9b1a6dc629
添加用户并设置随机密码
[root@sf106232 script]# cat 20170711.sh #!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-11 15:20 # Filename: 20170711.sh # Description: #**************************************************** . /etc/init.d/functions user="test" passfile="/tmp/user.log" for num in `seq -w 11 15` do pass="`echo "test$RANDOM" |md5sum |cut -c 3-11`" useradd $user$num &>/dev/null &&\ echo "$pass"|passwd --stdin $user$num &>/dev/null &&\ echo -e "user:$user$num\tpasswd:$pass" >>$passfile if [ $? -eq 0 ] then action "$user$num is ok" /bin/true else action "$user$num is false" /bin/false fi done echo -------------------------------------------------- cat $passfile && >$passfile
结果:
[root@sf106232 script]# sh 20170711.sh test01 is ok [ OK ] test02 is ok [ OK ] test03 is ok [ OK ] test04 is ok [ OK ] test05 is ok [ OK ] test06 is ok [ OK ] test07 is ok [ OK ] test08 is ok [ OK ] test09 is ok [ OK ] test10 is ok [ OK ] test11 is false [FAILED] test12 is false [FAILED] test13 is false [FAILED] test14 is false [FAILED] test15 is false [FAILED] -------------------------------------------------- user:test01 passwd:4254ee218 user:test02 passwd:162204f72 user:test03 passwd:a4bdacff8 user:test04 passwd:4fc2261da user:test05 passwd:b6cc21f98 user:test06 passwd:158e571a3 user:test07 passwd:d27c0fcb7 user:test08 passwd:2476823e5 user:test09 passwd:cbc65ba9e user:test10 passwd:c30c4a6ae
select 循环语句
语法结构:
select 变量名 [in 菜单取值列表]
do
指令
done
例子:
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-11 21:10 # Filename: 2017071101.sh # Description: #**************************************************** select name in cool360 cool360.org blog.cool360.org do echo $name done 测试: [root@sf106232 script]# sh 2017071101.sh 1) cool360 2) cool360.org 3) blog.cool360.org #?
采用数组做变量列表:
f106232 script]# cat 2017071101.sh #!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-11 21:10 # Filename: 2017071101.sh # Description: #**************************************************** array=(cool cool360 cool360.org) select name in "${array[@]}" do echo $name done 结果: [root@sf106232 script]# sh 2017071101.sh 1) cool 2) cool360 3) cool360.org #? 3 cool360.org #?
lnmp一键安装脚本select版
# Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-14 14:12 # Filename: lnmp.sh # Description: #**************************************************** RETVAR=0 path=/u01/scripts [ ! -d "$path" ] && mkdir $path -p function Usage() { echo "Usage:$0 argv" return 1 } function InstallService() { if [ $# -ne 1 ] ; then Usage fi local RETVAR=0 echo "start installing ${1}." sleep 2 ; if [ ! -x "$path/${1}.sh" ] ; then echo " $path/${1}.sh does not exist or can not be exec." return 1 else $path/$${1}.sh return $RETVAR fi } function main () { PS3="`echo pls input the num you want:`" select var in "Install lamp" "Install lnmp" "exit" do case "$var" in "Install lamp") InstallService lamp RETVAR=$? ;; "Install lnmp") InstallService lnmp RETVAR=$? ;; exit) echo bye. return 3 ;; *) echo "the num you input must be {1|2|3}" echo "Input ERROR." esac done exit $RETVAR } main
测试:
[root@sf106232 scripts]# sh lnmp.sh 1) Install lamp 2) Install lnmp 3) exit pls input the num you want:2 start installing lnmp. /u01/scripts/lnmp.sh does not exist or can not be exec. pls input the num you want:
方法2.
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-14 14:12 # Filename: lnmp.sh # Description: #**************************************************** RETVAR=0 path=/u01/scripts [ ! -d "$path" ] && mkdir $path -p function Usage() { echo "Usage:$0 argv" return 1 } function InstallService() { if [ $# -ne 1 ] ; then Usage fi local RETVAR=0 echo "start installing ${1}." sleep 2 ; if [ ! -x "$path/${1}.sh" ] ; then echo " $path/${1}.sh does not exist or can not be exec." return 1 else $path/$${1}.sh return $RETVAR fi } function main () { PS3="`echo pls input the num you want:`" select var in "Install lamp" "Install lnmp" "exit" do case "$REPLY" in 1) InstallService lamp RETVAR=$? ;; 2) InstallService lnmp RETVAR=$? ;; 3) echo bye. return 3 ;; *) echo "the num you input must be {1|2|3}" echo "Input ERROR." esac done exit $RETVAR } main sh lnmp.sh 1) Install lamp 2) Install lnmp 3) exit pls input the num you want:1 start installing lamp. /u01/scripts/lamp.sh does not exist or can not be exec. pls input the num you want:2 start installing lnmp. /u01/scripts/lnmp.sh does not exist or can not be exec. pls input the num you want:3 bye.
备注:select有两个重要的变量,一个是PS3,一个是$REPLY.
PS3:打印提示信息。
PEPLY:获取菜单对应的数字,用来在case里调用。
PS3:打印提示信息。
PEPLY:获取菜单对应的数字,用来在case里调用。
循环控制状态返回值实践
给网卡配置子ip.
例子: #!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-14 19:59 # Filename: ip_add_del.sh # Description: #**************************************************** #!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions RETVAL=0 op() { if [ "$1" == "del" ] ; then list=`echo {16..1}` else list=`echo {1..16}` fi for ip in $list do if [ $ip -eq 10 ] then continue fi ip addr $1 10.0.2.$ip/24 dev eth0 label eth0:$ip &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ] then action "$1 $ip" /bin/true else action "$1 $ip" /bin/false fi done return $RETVAL } case "$1" in start) op add RETVAL=$? ;; stop) op del RETVAL=$? ;; restart) op del sleep 2 op add RETVAL=$? ;; *) echo $"USAGE:$0 {start|stop|restart}" esac exit $RETVAL
[root@sf106232 scripts]# sh ip_add_del.sh start add 1 [ OK ] add 2 [ OK ] add 3 [ OK ] add 4 [ OK ] add 5 [ OK ] add 6 [ OK ] add 7 [ OK ] add 8 [ OK ] add 9 [ OK ] add 11 [ OK ] add 12 [ OK ] add 13 [ OK ] add 14 [ OK ] add 15 [ OK ] add 16 [ OK ] [root@sf106232 scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2 inet addr:10.19.106.232 Bcast:10.19.106.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:26215297 errors:0 dropped:0 overruns:0 frame:0 TX packets:16254411 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2901795004 (2.7 GiB) TX bytes:2101024325 (1.9 GiB) eth0:1 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2 inet addr:10.0.2.1 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:2 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2 inet addr:10.0.2.2 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:3 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2 inet addr:10.0.2.3 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:4 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2 inet addr:10.0.2.4 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:5 Link encap:Ethernet HWaddr 00:50:56:9C:3B:F2
MD5sum加密后破解
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https://bk.devopstack.cn # Email: 258818040@qq.com # Last modified: 2017-07-14 21:18 # Filename: md5sum.sh # Description: #**************************************************** for n in {0..32767} do echo "`echo $n |md5sum` $n" >> /tmp/zhiwen.log done md5char="04fe8bf20edfc" while read line do if [ `echo $line|grep "$md5char"|wc -l ` -eq 1 ] then echo $line break fi done </tmp/zhiwen.log [root@sf106232 scripts]# sh md5sum.sh 1dcca23355272056f04fe8bf20edfce0 - 5 [root@sf106232 scripts]# sh md5sum.sh
shell中的数组
语法:
array=( value1 value2 value3 ..)
[root@sf106232 scripts]# array=(1 2 3) [root@sf106232 scripts]# echo ${array[*]} 1 2 3 [root@sf106232 scripts]# echo ${array[@]} 1 2 3 [root@sf106232 scripts]# array=([1]=one [2]=two [3]=3) [root@sf106232 scripts]# echo ${array[2]} two [root@sf106232 scripts]# echo ${array[3]} 3 [root@sf106232 scripts]# echo ${array[1]} one 动态定义数组变量 [root@sf106232 scripts]# array=(`ls `) [root@sf106232 scripts]# echo ${array[@]} ip_add_del.sh lnmp1.sh lnmp.sh md5sum.sh [root@sf106232 scripts]# echo ${array[1]} lnmp1.sh [root@sf106232 scripts]# echo ${array[0]} ip_add_del.sh、 数组赋值: [root@sf106232 scripts]# array[4]=testtest [root@sf106232 scripts]# echo ${array[*]} ip_add_del.sh lnmp1.sh lnmp.sh md5sum.sh testtest 数组删除: [root@sf106232 scripts]# unset array[1] [root@sf106232 scripts]# echo ${array[*]} ip_add_del.sh lnmp.sh md5sum.sh testtest 删除整个数组 [root@sf106232 scripts]# unset array [root@sf106232 scripts]# echo ${array[*]} 数组截取 [root@sf106232 scripts]# array=(1 2 3 4 5) [root@sf106232 scripts]# echo ${array[@]:0:2} 1 2 数组替换 [root@sf106232 scripts]# echo ${array[@]} 1 2 3 4 5 [root@sf106232 scripts]# echo ${array[@]/2/c} 1 c 3 4 5 [root@sf106232 scripts]# echo ${array[@]/1/c} c 2 3 4 5 [root@sf106232 scripts]# array=(1 2 3 4 5 5 5) [root@sf106232 scripts]# echo ${array[@]/5/c} 1 2 3 4 c c c [root@sf106232 scripts]# cat array.sh #!/bin/bash array=(a b c d e) for((i=0; i<${#array[@]};i++)) do echo ${array[i]} done [root@sf106232 scripts]# sh array.sh a b c d e [root@sf106232 scripts]# cat array.sh #!/bin/bash array=(a b c d e) i=0 while ((i<${#array[@]})) do echo ${array[i]} ((i++)) done [root@sf106232 scripts]# array=($(ls)) [root@sf106232 scripts]# echo ${array[@]} array.sh ip_add_del.sh lnmp1.sh lnmp.sh md5sum.sh
mysql主从检查报警脚本
#!/bin/bash path=/u02/scripts MAIL_GROUP="258818040@qq.com 258818041@qq.com" PAGER_GROUP="1111111111 22222222222" LOG_FILE="/tmp/web_check.log" USER=root PASSWORD=test PORT=3306 MYSQLCMD="mysql -u$USER -p$PASSWORD -S /data/$PORT/mysql.sock" error=(1008 1007 1062) RETVAL=0 [ ! -d $path ] && mkdir -p $path function JudgeError(){ for((i=0;i<${#error[*]};i++)) do if [ "$1" == "${error[$i]}" ] then echo "MySQL slave errorno is $1,auto repairing it." $MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;" fi done return $1 } function CheckDb(){ status=($(awk -F ': ' '/_Running|Last_Errno|_Behind/{print $NF}' slave.log)) expr ${status[3]} + 1 &>/dev/null if [ $? -ne 0 ];then status[3]=300 fi if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a ${status[3]} -lt 120 ] then #echo "Mysql slave status is ok" return 0 else #echo "mysql replcation is failed" JudgeError ${status[2]} fi } function MAIL(){ local SUBJECT_CONTENT=$1 for MAIL_USER in `echo $MAIL_GROUP` do mail -s "$SUBJECT_CONTENT " $MAIL_USER <$LOG_FILE done } function PAGER(){ for PAGER_USER in `echo $PAGER_GROUP` do TITLE=$1 CONTACT=$PAGER_USER HTTPGW= #send_message method1 curl -d done } function SendMsg(){ if [ $1 -ne 0 ] then RETVAL=1 NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"` SUBJECT_CONTENT="mysql slave is error,errorno is $2,${NOW_TIME}." echo -e "$SUBJECT_CONTENT"|tee $LOG_FILE MAIL $SUBJECT_CONTENT PAGER $SUBJECT_CONTENT $NOW_TIME else echo "Mysql slave status is ok" RETVAL=0 fi return $RETVAL } function main(){ while true do CheckDb SendMsg $? sleep 300 done } main
sh -n 可以调试脚本语法
sh -v
sh -x
set -n 读命令不执行
set -v 显示读取的所有行
set -x 显示所有命令和参数,一般写在脚本里面开启,set +x 终止调试。可以局部调试。
sh -v
sh -x
set -n 读命令不执行
set -v 显示读取的所有行
set -x 显示所有命令和参数,一般写在脚本里面开启,set +x 终止调试。可以局部调试。
[root@sf106232 scripts]# sh -n lnmp.sh lnmp.sh: line 73: syntax error: unexpected end of file 改正之后: [root@sf106232 scripts]# vim lnmp.sh [root@sf106232 scripts]# sh -n lnmp.sh
注释:用vim编辑器 在Esc模式下 可以按v限定调整格式的行数然后按=号调整代码的格式。操作不可逆。
点击可以展开查看历史记录
嗨、骚年、快来消灭0回复。