一:while循環(huán)
while <條件表達式> #此處可以是(())青扔、[]毒返、[[]]和前面條件表達式判斷一樣
do
指令..
done
while循環(huán)主要是
1.重復執(zhí)行一組命令,常常用于守護進程或無限循環(huán)的程序(要加sleep和usleep控制頻率)凉敲。
2.適用于頻率小于1分鐘的循環(huán)處理,其他的都可以用crond功能代替
注:
sleep 1 # 休息一秒
usleep 1000000 # 休息1000000微秒,即休息一秒
例一:
#!/bin/bash
while [ 1 ] #[ 1 ]和true 一樣爷抓,只是前面2邊需要有空格
do
uptime >>/tmp/uptime.log
sleep 2
done
#uptime用于查看系統(tǒng)負載势决,具體可參見http://os.51cto.com/art/201312/420840.htm
例二:計算1+2...100的和
方式一:
#!/bib/sh
SUM=0
I=1
while [ $I -le 100 ] #[]中需要變量取值符號"$",而且大于小于只能用"-le"
do
((SUM+=I))
let I++
done
echo $SUM
-------------------------------------------------------------------------------------------------
方式二:
#!/bib/sh
SUM=0
I=1
while ((I<=100)) #(())中可以不要變量取值符號,大于小于是可以直接用
do
((SUM+=I))
((I++))
done
echo $SUM
-------------------------------------------------------------------------------------------------
方式三:
#!/bib/sh
SUM=0
I=100
((SUM=I*(I+1)/2))
echo $SUM
實戰(zhàn)一
手機充值10元蓝撇,每發(fā)一次短信花費1角5分果复,當余額低于1角5分時就不能在發(fā)短信,并且提示"余額不足渤昌,請充值",用while實現(xiàn)
簡單版一
#!/bin/bash
sum=1000
fee=15
while [ $sum -gt $fee ]
do
echo "send message successfully"
((sum-=fee))
echo "left $sum"
done
echo "余額不足虽抄,請充值"
--------------------------------------------------------------------------------------------
專業(yè)版二:
#!/bin/bash
export LANG="zh_CN.UTF-8" #定義中文字符防止亂碼
sum=15 #總錢數(shù)
msg_fee=15 #每條短信的錢數(shù)
function menu(){
cat <<END
當前余額${sum},每條短信需要${msg_fee}
==============
1.充值余額
2.發(fā)送短信
3.退出
==============
END
}
#充錢
function recharge(){
read -p "請輸入充值金額:" money
expr $money + 1 &>/dev/null
if [ $? -ne 0 ];then
echo "must be int"
else
((sum+=money))
echo "您的余額為$sum"
fi
}
#發(fā)信息
function sendInfo(){
if ((sum < nsg_fee));then #先判斷錢夠不夠
echo "您的余額不足,請充值"
else
while true
do
read -p "請輸入內容(不要帶空格):" message
echo "$message 已發(fā)送"
((sum-=msg_fee))
return 0 #此處發(fā)送完成后独柑,要去菜單選擇极颓,而不是一直發(fā)短信
if [ $sum -lt $msg_fee ];then
printf "溫馨提示:您的余額不足"
return 1
fi
done
fi
}
function main(){
while true #一直顯示菜單,若想退出數(shù)字"3"群嗤,即可退出
do
menu
read -p "請輸入一個數(shù)字:" NUM
case "$NUM" in
1)
recharge
;;
2)
sendInfo
;;
3)
exit 0
;;
*)
echo "must be {1|2|3}"
esac
done
}
main
效果如圖所示:
[centos@mycentos iphone_fee]$ sh 2.sh
當前余額15,每條短信需要15
==============
1.充值余額
2.發(fā)送短信
3.退出
==============
請輸入一個數(shù)字:1
請輸入充值金額:100
您的余額為115
當前余額115,每條短信需要15
==============
1.充值余額
2.發(fā)送短信
3.退出
==============
請輸入一個數(shù)字:2
請輸入內容(不要帶空格):hello
hello 已發(fā)送
當前余額100,每條短信需要15
==============
1.充值余額
2.發(fā)送短信
3.退出
==============
請輸入一個數(shù)字:3
[centos@mycentos iphone_fee]$
注:case常用于服務啟動腳本中,常用cat的here文檔的方式打印菜單
實戰(zhàn)二
用while守護進程的方式去監(jiān)控網(wǎng)站兵琳,每10秒確定一次是否正常狂秘,若不正常,就發(fā)郵箱通知
預備知識:
curl -I www.qq.com : 顯示響應頭部信息
curl -o 1.txt www.qq.com : 將網(wǎng)頁下載到1.txt文件中
curl命令解釋:
curl -o /dev/null -s -w %{http_code} http://zys.8800.org/
-o 參數(shù)躯肌,是把下載的所有內容都重定向到/dev/null者春,-s命令,是屏蔽了curl本身的輸出清女,而-w參數(shù)钱烟,是根據(jù)我們自己的需要,自定義了curl的輸出格式嫡丙。
用上面的命令采集頁面的狀態(tài)碼拴袭,返回200表示頁面正常曙博,其他返回碼則是異常拥刻。
代碼如下:
#!/bin/bash
if [ $# -ne 1 ];then
echo $"usage:$0 url"
exit 1
fi
while true #守護進程
do
#取回頁面的狀態(tài)碼
if [ $( curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $1 | egrep -w "200|301|302" | wc -l) -ne 1 ];then
echo "$1 is error"|mail -s "$1 is error" 111111@qq.com
else
echo "$1 is ok"
fi
sleep 10
done
知識點補充:
1、當運行腳本時父泳,若突然中斷般哼,優(yōu)肯會導致數(shù)據(jù)的丟失,則需要防止中斷的方式
1.使用"sh shell.sh &"命令 惠窄,即使用&在后臺運行
2.使用"nohup shell.sh &"命令蒸眠,即使用nohup 加&在后臺運行腳本
3.利用screen保持會話,然后再執(zhí)行腳本杆融,即使用screen保持當前會話
后臺運行的知識:
sh shell.sh & 腳本shell.sh 放在后臺執(zhí)行
Ctrl+c 停止執(zhí)行當前腳本或任務
Ctrl+z 暫停執(zhí)行當前腳本
jobs 查看當前執(zhí)行的腳本或任務
bg 將當前腳本或任務放到后臺執(zhí)行
kill 關閉當前腳本任務楞卡,即以"kill %號碼"的形式關閉進程,任務號通過jobs獲得
fg 將當前的腳本或任務放到前臺執(zhí)行,若有多任務臀晃,則"fg 號碼"調出相應的任務編號
效果如圖:
[centos@mycentos shell]$ sh 2.sh & #后臺運行
[1] 2988
[centos@mycentos shell]$ jobs #查看任務數(shù)
[1]+ Running sh 2.sh &
[centos@mycentos shell]$ kill %1 #關閉進程為1的進程
[centos@mycentos shell]$ jobs
[1]+ Terminated sh 2.sh #程序已經(jīng)被關閉
2.while按行讀取文件的幾種方式
1).
exec <FILE
sum=0
while read line
do
cmd
done
---------------------------------------------------------------------------------------------------
2).
cat file | while read line
do
cmd
done
-------------------------------------------------------------------------------------------------
3).while read line
do
cmd
done<file
例如:開發(fā)一個shell腳本實現(xiàn)cat讀文件的基本功能
#!/bin/bash
while read line
do
echo $line
done<$1
二:for循環(huán)
for 變量名 in 變量取值列表
do
指令
done
注:讀取取值列表時默認以空格分割
C語言版循環(huán)
for ((exp1;exp2;exp3))
do
指令觉渴。。徽惋。
done
例如:
for ((i=0;i<=3;i++))
do
echo $i
done
注:"in 變量取值列表" 可以省略案淋,省略時相當于in "$@",即for i 就等于 for i in "$@"
例一:打印5、4险绘、3踢京、2、1這五個數(shù)字
方式一:
#!/bin/bash
#直接列出變量列表宦棺,打印5瓣距、4、3代咸、2蹈丸、1 以空格為分隔符
for NUM in 5 4 3 2 1
do
echo $NUM
done
-------------------------------------------------------------------------------------------------
方式二:
#!/bin/sh
#用{}號實現(xiàn)
for NUM in {5..1}
do
echo $NUM
done
-------------------------------------------------------------------------------------------------
方式三:
#!/bin/bash
#5是起始數(shù)字 -1是步長,即每次減一 1是結束數(shù)字
for NUM in $(seq 5 -1 1)
do
echo $NUM
done
實戰(zhàn)三:批量更改文件名呐芥,將目錄下以".txt"結尾的全部變成".gif"
思路:先處理一個逻杖,再批量處理
[centos@mycentos test]$ ll
total 0
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 1.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 2.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 3.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 4.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 5.txt
1.先處理一個:
[centos@mycentos test]$ file=1.txt
[centos@mycentos test]$ echo $file
1.txt
[centos@mycentos test]$ echo $file | cut -d '.' -f1
1
[centos@mycentos test]$ echo $(echo $file | cut -d '.' -f1).gif #得到要變成的樣子
1.gif
[centos@mycentos test]$ mv $file $(echo $file | cut -d '.' -f1).gif
[centos@mycentos test]$ ll
total 0
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 1.gif
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 2.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 3.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 4.txt
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 5.txt
2.腳本批量處理:
#!/bin/bash
for file in $(ls | grep "txt$")
do
mv ${file} $(echo $file | cut -d . -f1).gif
done
結果:
total 0
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 1.gif
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 2.gif
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 3.gif
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 4.gif
-rw-rw-r--. 1 centos centos 0 Nov 13 13:58 5.gif
實戰(zhàn)四:
在linux下批量修改文件名,將下列文件名中"_finished"去掉
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_1_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_2_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_3_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_4_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_5_finished.jpg
方法一:
先處理一個后批量處理
#!/bin/sh
for list in $(ls *.jpg)
do
mv $list $(echo $list |sed 's/_finished//g')
done
---------------------------------------------------------------------------------------------
方法二:(awk)
[centos@mycentos old]$ ls | awk -F '.' '{print $0,$1,$2}' #$0為本身
ch.sh ch sh
sku_102999_1.jpg sku_102999_1 jpg
sku_102999_2.jpg sku_102999_2 jpg
sku_102999_3.jpg sku_102999_3 jpg
sku_102999_4.jpg sku_102999_4 jpg
sku_102999_5.jpg sku_102999_5 jpg
[centos@mycentos old]$ ls | awk -F '.' '{print $0,$1"_finished."$2}' #進行拼接
ch.sh ch_finished.sh
sku_102999_1.jpg sku_102999_1_finished.jpg
sku_102999_2.jpg sku_102999_2_finished.jpg
sku_102999_3.jpg sku_102999_3_finished.jpg
sku_102999_4.jpg sku_102999_4_finished.jpg
sku_102999_5.jpg sku_102999_5_finished.jpg
[centos@mycentos old]$ ls | awk -F '.' '{print "mv", $0,$1"_finished."$2}'
mv ch.sh ch_finished.sh
mv sku_102999_1.jpg sku_102999_1_finished.jpg
mv sku_102999_2.jpg sku_102999_2_finished.jpg
mv sku_102999_3.jpg sku_102999_3_finished.jpg
mv sku_102999_4.jpg sku_102999_4_finished.jpg
mv sku_102999_5.jpg sku_102999_5_finished.jpg
[centos@mycentos old]$ ls | awk -F '.' '{print "mv", $0,$1"_finished."$2}' | bash #將字符交給bash處理
[centos@mycentos old]$ ll
total 4
-rw-rw-r--. 1 centos centos 93 Nov 13 14:37 ch_finished.sh
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_1_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_2_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_3_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_4_finished.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_5_finished.jpg
-------------------------------------------------------------------------------------------------
方法三:
[centos@mycentos old]$ rename "_finished" "" *jpg #不要忘了"" 思瘟,空格代表去除
[centos@mycentos old]$ ll
total 4
-rw-rw-r--. 1 centos centos 93 Nov 13 14:37 ch_finished.sh
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_1.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_2.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_3.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_4.jpg
-rw-rw-r--. 1 centos centos 0 Nov 13 14:31 sku_102999_5.jpg
rename 例子補充:
批量去掉文件名中的"bd"
[centos@mycentos db]$ ll
total 0
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 bd02.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 bd03.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 bd04.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 bd05.html
rename方式:
[centos@mycentos db]$ rename "bd" "" *.html
[centos@mycentos db]$ ll
total 0
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 02.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 03.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 04.html
-rw-rw-r--. 1 centos centos 0 Nov 13 14:51 05.html
注:
先去掉一個荸百,后批量去除也可
實戰(zhàn)五:通過腳本實現(xiàn)僅僅sshd,network,crond,sysstat,rsyslog服務在開機時自啟動,chkconfig --list是查看所有服務開機時的情況滨攻。
方式一:
先統(tǒng)一關閉够话,后開啟特定的
for old in $(chkconfig --list | grep "3:on" | awk '{print $1}' )
do
chkconfig --level 3 $old off
done
for old in crond network rsyslog sysstat sshd
do
chkconfig --level 3 $old on
done
-------------------------------------------------------------------------------------------------
方式二:
因為默認情況下開機需要保留的服務都是開啟,所以將其他的關閉就好
for old in $(chkconfig --list | grep "3:on" |awk '{print $1}' egrep -v "crond|network|rsyslog|sshd|sysstat")
do
chkconfig --level 3 $old off
done
-------------------------------------------------------------------------------------------------
方式三:
awk的拼接
chkconfig --list | egrep -v " sshd|crond|rsyslog|sysstat|network" |awk '{print "chkconfig", $1, "off"}' |bash
效果如圖:
[root@mycentos ~]# chkconfig --list | egrep -v "
sshd|crond|rsyslog|sysstat|network" |awk '{print "chkconfig", $1, "off"}' #awk中的","變成顯示后的空格進行分割
chkconfig wdaemon off
chkconfig winbind off
chkconfig wpa_supplicant off
chkconfig xinetd off
chkconfig ypbind off
.....
chkconfig --list | egrep -v " sshd|crond|rsyslog|sysstat|network" |awk '{print "chkconfig", $1, "off"}' | bash #將上面的當做命令傳輸?shù)絙ash運行
可以簡化成:
chkconfig | egrep -v " sshd|crond|rsyslog|sysstat|network" |awk '{print "chkconfig", $1, "off"}' | bash
實戰(zhàn)六:打印九九乘法表
#!/bin/sh
COLOR='\E[47;30m'
RES='\e[0m'
for line in $(seq 9)
do
for col in $(seq ${line})
do
if (((line*col)>9));then #此處用來控制輸出格式光绕,當是大于9時后面是一個空格女嘲,小于等于9時就是2個空格
echo -en "${COLOR}${col}*${line}=$((line*col))${RES} " #一個空格 -e用來顯示文字,-n為了不換行
else
echo -en "${COLOR}${col}*${line}=$((line*col))${RES} " #2個空格
fi
done
echo ' ' #一行完成后換行
done
效果如圖:
實戰(zhàn)七:
批量創(chuàng)建10個系統(tǒng)賬號(oldboy01-oldboy10),并且設置密碼(密碼是隨機數(shù)诞帐,要求是字符和數(shù)字的混合)
思路:
1.創(chuàng)建賬號
seq -w 10:表示 01-10 :格式對齊
或者
{01..10}
2.創(chuàng)建賬戶澡为,設置無交互密碼
useradd oldboy
echo 111111 | passwd --stdin oldboy
3.隨機八位密碼(字母數(shù)字混合)
小插曲:
生成隨機數(shù)的方式
1.利用RANDOM(范圍是0-32767)隨機生成數(shù)字,然后利用md5進行加密景埃,取5到12為字符
[root@mycentos ~]# echo $RANDOM |md5sum | cut -c 5-12
1684ebbf
若只有RANDOM媒至,則安全性不高,$RANDOM前面加上自己定制的秘鑰谷徙,安全就會增加
echo "oldboy$RANDOM" 此時的就無法破解
2.利用OpenSSL生成隨機數(shù)
[root@mycentos shell]# openssl rand -base64 8
FAQco/00NL0=
[root@mycentos shell]# openssl rand -base64 80
s0KqTLEA6fIueDcsJkPaqzd3owXNVUcN+d+9FyiMn1sXYShjmsDgbzrndvrn9o8i
ZbjkSZzahr1ZMMbLcqJ/DvCfMAhEcZyG/hIvKMzkr8c=
3.利用/dev/urandom ,這里記錄系統(tǒng)此時狀態(tài)拒啰,轉換成數(shù)字
[root@mycentos shell]# head /dev/urandom | cksum
434988922 2930
[root@mycentos shell]# head /dev/urandom | cksum
3283962471 2161
4.通過日期生成隨機數(shù);
[root@mycentos shell]# date +%s%N
1542160944683910406
以上的隨機數(shù)長短不一,通過md5sum進行統(tǒng)一格式
1.echo "test$RANDOM" |md5sum|cut -c 2-9
2.openssl rand -base64 80 | md5sum|cut -c 2-9
3.date +%s%N |md5sum|cut -c 2-9
4.head /dev/urandom | md5sum |cut -c 2-9
方式一:
#!/bin/bash
user="oldboy"
passfile="/tmp/user1.log"
for num in $(seq -w 10)
do
useradd $user$num #創(chuàng)建用戶
pass="$(echo "test$RANDOM" |md5sum|cut -c 3-11)" #先用秘鑰加密后取得密碼完慧,用變量保存
echo "$pass"|passwd --stdin $user$num &>/dev/null #將密碼賦值給用戶
echo -e "user:$user$num\tpasswd:$pass">>$passfile #將用戶信息放到固定文件中谋旦,-e參數(shù)處理特殊字符,此處將"\t"處理成tab效果,而不
是直接輸出"\t"
done
echo "------------------------------------------------------"
cat $passfile
特別注意:RANDOM是一個隨機數(shù)册着,每次都不一樣拴孤,因此如果需要用到2次,就一定要先用變量保存
------------------------------------------------------------------------------------------------
方式二:
用chpasswd批量創(chuàng)建密碼
批量創(chuàng)建密碼:用命令chpasswd
[root@mycentos shell]# useradd old
[root@mycentos shell]# echo "old:123456"|chpasswd
[root@mycentos shell]# su - oldgirl #root切換成普通用戶不需要密碼
[oldgirl@mycentos ~]$ su - old
Password:
[old@mycentos ~]$ whoami
old
給多個用戶設置密碼:
chpasswd < 密碼文件
其中密碼文件格式:
用戶名1:口令1
用戶名2:口令2
前提:用戶必須存在
#!/bin/bash
. /etc/init.d/functions
user="xioaming"
passfile="/tmp/user.log"
for num in $(seq -w 10) #批量創(chuàng)建用戶和密碼
do
pass="$(echo "test$RANDOM" |md5sum|cut -c 3-11)"
useradd ${user}${num} &>/dev/null && \
echo -e "${user}${num}:$pass" >> $passfile
if [ $? -eq 0 ];then
action "${user}${num} is ok" /bin/true
else
action "${user}${num} is fail" /bin/false
fi
done
echo "-------------------------------------------------"
chpasswd < $passfile #chpasswdd 用于批量給用戶設置密碼甲捏,此處將上面批量設置的用戶和密碼一一對應
cat$passfile && > $passfile
實戰(zhàn)八:
打印選擇菜單演熟,按照選項意見按照不同的web服務
[root@mycentos select]# sh menu.sh
1.[install lamp]
2.[install lanp]
3.exit
input the num you want:工作中所用的lamp
要求:
1.當用戶輸入1時,輸出"start installing lamp"提示司顿,然后執(zhí)行/server/scripts/lamp.sh輸出"lamp is installed",并且退出腳本芒粹,此為工作中所用的lamp一鍵安裝腳本
2.當用戶輸入2時,輸出"start installing lnmp" 提示大溜,然后執(zhí)行/server/scripts/lnmp.sh輸出"lnmp is installed",并退出腳本化漆,此為工作中的lnmp一鍵安裝腳本
3.當輸入3時,退出當前菜單及腳本提示
4.當輸入任何其他字符時钦奋,給出"Input error"后退出腳本
5.對執(zhí)行腳本進行先關的條件判斷座云,例如:腳本文件是否存在,是否可執(zhí)行等的判斷付材。
[root@mycentos select]# echo "lanp is installed" > /server/scripts/lanp.sh
[root@mycentos select]# echo "lamp is installed" > /server/scripts/lamp.sh
#!/bin/sh
path=/server/scripts #設置腳本文件的路徑
[ ! -d "$path" ]&& mkdir -p $path #判斷是否存在朦拖,不存在就創(chuàng)建
function menu(){ #菜單欄
cat <<END
1.[install lamp]
2.[install lanp]
3.exit
input the num you want:
END
}
function main(){ #主函數(shù)
menu #顯示菜單
read num #用戶選項
expr $num + 1 &> /dev/null # 判斷用戶輸入是否是整數(shù)
if [ $? -ne 0 ];then
echo "must input {1|2|3}"
exit 1
fi
case "$num" in
1)
echo "start installing lamp"
sleep 2
if [ -x "$path/lamp.sh" ];then #判斷腳本是否可執(zhí)行
source $path/lamp.sh #加載其他腳本
exit $?
else
echo "$path/lamp.sh does not exist or can not be exec"
exit 1
fi
;;
2)
echo "start installing lanp"
sleep 2
if [ -x "$path/lanp.sh" ];then
source $path/lanp.sh
exit $?
else
echo "$path/lamp.sh does not exist or can not be exec"
exit 1
fi
;;
3)
echo "bye"
exit 3
;;
*)
echo "you must input {1|2|3}"
esac
}
main
注:
1.此處用的是cat用法,還可以用select伞租,但是常用的是cat 的here文檔