標籤:linux shell centos shell編程
1、 if 判斷一些特殊用法
if [ -z $a ] 這個表示當變數a的值為空白時會怎麼樣
if [ ! -e file ]; then 表示檔案不存在時會怎麼樣
if (($a<1)); then …等同於 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=這樣的符號
$3不存在,所以n為空白;判斷是否為空白;
[[email protected] ~]# n=`wc -l /etc/passwd|awk ‘{print $3}‘`[[email protected] ~]# echo $n[[email protected] ~]# if [ -z $n ];then echo "\$n is null";fi$n is null
if grep -q ‘123‘ 1.txt; then 表示如果1.txt中含有‘123‘的行時會怎麼樣?
1.txt中含有123;grep -q ‘123‘ 1.txt 匹配OK,傳回值為真;
[[email protected] shell]# cat 1.txt123sdwe[[email protected] shell]# if grep -q "123" 1.txt ;then echo kong;fikong
2、 shell中的case判斷
格式: case 變數名 in value1) command ;; value2) command ;; *) commond ;; esac在case程式中,可以在條件中使用 |,表示或的意思, 比如2|3) command ;;當變數為2或者3時,執行該部分命令。
/etc/init.d/naginx 裡面有case語句;匹配輸入的第一個參數是否為start stop reload restart conifgtest,輸入其他字元則返回
Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest}
case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1esac
舉例,輸入的是字母,提示輸入一個純數字,輸入數字判斷是偶數或奇數;
[[email protected] 0618]# cat case1.sh #!/bin/bash#要求輸入的是數字,判斷奇數或偶數;非數字則提示輸入數字,然後退出;read -p "please input a number:" nn1=`echo $n|sed ‘s/[0-9]//g‘`#輸入為數字則sed替換為空白,傳回值為空白;輸入為字母則傳回值不為空白;if [ ! -z $n1 ]then echo "please input a number " exit 1fin2=$[$n%2]case $n2 in 0) echo "偶數" ;; 1) echo "奇數" ;; *) echo "不存在" ;;esac
[[email protected] 0618]# sh -x case1.sh + read -p ‘please input a number:‘ nplease input a number:de2++ echo de2++ sed ‘s/[0-9]//g‘+ n1=de+ ‘[‘ ‘!‘ -z de ‘]‘+ echo ‘please input a number ‘please input a number + exit 1[[email protected] 0618]# sh case1.sh please input a number:234偶數[[email protected] 0618]# sh case1.sh please input a number:weplease input a number
case實驗2:輸入為空白則提示輸入數字然後退出;輸入的數字在0-100內判斷成績;輸入為非數字則提示輸入數字然後退出;
輸入負數和大於100的都會提示輸入的數字為0-100;
#!/bin/bash#case實驗,輸入為空白提示輸入數字然後退出;輸入的數字在0-100內判斷成績;輸入的非數字提示輸入數字然後退出;read -p "please input a number:" nif [ -z $n ]then echo "Please input a number" exit 1fin1=`echo $n|sed ‘s/[-0-9]//g‘`#sed替換加了-表示負數;if [ ! -z $n1 ]then echo "please input a number " exit 1fiif [ $n -ge 0 ] && [ $n -lt 60 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "不及格" ;; 2) echo "及格" ;; 3) echo "良好" ;; 4) echo "優秀" ;; *) echo "輸入的數字為0-100" ;;esac
[[email protected] 0618]# sh case2.sh please input a number:-200輸入的數字為0-100[[email protected] 0618]# sh case2.sh please input a number:101輸入的數字為0-100
3、 shell指令碼中的迴圈
for迴圈 文法結構:for 變數名 in 條件; do … done
while 迴圈文法結構: while 條件; do … done 死迴圈用:表示
break直接結束本層迴圈; continue忽略continue之下的代碼,直接進行下一次迴圈
exit 直接退出shell
for迴圈實驗:列出/etc目錄下所有的目錄
[[email protected] ~]# cat for.sh #!/bin/bashfor f in `ls /etc/`doif [ -d /etc/$f ]then ls -d "/etc/$f"fidone
while迴圈實驗:判斷負載的迴圈;
[[email protected] ~]# cat load.sh #!/bin/bash#監測負載的指令碼,取w負載1分鐘內的負載值如果大於10,則30秒發一次郵件;while :doload=`w |head -1 |awk -F "load average: " ‘{print $2}‘|cut -d. -f1`if [ $load -gt 10 ]then top|mail -s "load is high:$load" [email protected]else exit 0fi sleep 30done
while迴圈實驗:
如果輸入為空白,提示要求輸入東西,如果輸入字元提示輸入純數字,輸入純數字列印數字,退出;
[[email protected] 0618]# cat while.sh #!/bin/bash#輸入為空白,提示輸入東西一直到輸入不為空白結束;如輸入的是字母則提示只能輸入一個純數字,直到輸入純數字為止,列印數字結束;while :do read -p "please input a number:" n if [ -z $n ] then echo "請輸入一個東西" continuefi n1=`echo $n | sed ‘s/[-0-9]//g‘` if [ ! -z $n1 ] then echo "請輸入一個純數字" continuefi breakdoneecho $n
continue 退出本次迴圈;迴圈內部繼續,不執行迴圈後面的了;
break跳出整個迴圈,迴圈後面的還會執行。
exit的話退出整個指令碼;
break實驗:條件匹配的話退出整個迴圈;
[[email protected] 0618]# cat break.sh #!/bin/bash#break實驗;條件匹配的話退出整個迴圈;迴圈後面的還會執行;for i in `seq 1 5`do echo $i if [ $i == 3 ]then break fiecho $idoneecho OK
[[email protected] 0618]# sh break.sh11223OK
continue實驗;條件匹配的話退出本次迴圈,繼續執行迴圈;
#!/bin/bashfor i in `seq 1 5`do echo $i if [ $i == 3 ] then continue fi echo $idoneecho OK
[[email protected] 0618]# sh continue.sh 112234455OK
exit實驗;條件匹配的話退出整個指令碼;
#!/bin/bashfor i in `seq 1 5`do echo $i if [ $i == 3 ] then exit 1 fi echo $idoneecho OK
[[email protected] 0618]# sh break.sh 11223
4、shell中的函數
函數就是把一段代碼整理到了一個小單元中,並給這個小單元起一個名字,當用到這段代碼時直接調用這個小單元的名字即可。
格式: function f_name() {
command
}
函數必須要放在最前面
函數裡可以export 全域變數;
函數實驗1:定義input函數,輸入一個字元,則列印一個字元;
#!/bin/bashinput(){ echo $1 }input yonglinux[[email protected] ~]# sh 1.sh yonglinux
函數實驗2:定義sum函數,進行求和運算;
#!/bin/bashsum() { s=$[$1+$2] echo $s}sum 1 2[[email protected] 0618]# sh 2.sh 3
查看ip地址的函數;
#!/bin/bash#查看ip地址的函數;輸入一個網卡名,輸出網卡對應的ip地址;$1為互動時輸入的網卡名;ip() { ifconfig |grep -A1 "$1"|tail -1|awk ‘{print $2}‘|awk -F ‘:‘ ‘{print $2}‘}read -p "please input the eth name:" emyip=`ip $e`echo "$e address is $myip"
[[email protected] 0618]# sh 2.sh please input the eth name:eth0eth0 address is 192.168.11.100[[email protected] 0618]# sh 2.sh please input the eth name:eth1eth1 address is 192.168.20.100[[email protected] 0618]# sh 2.sh please input the eth name:lolo address is 127.0.0.1
本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1664557
linux下shell指令碼編程2