生活随笔
收集整理的這篇文章主要介紹了
Shell编程入门(第二版)(下)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
流程控制語句
三、select/in[較少用]
格式:
[python]?view plaincopy
????select?[變量]?in?[關鍵字]??????do???????????command?1???????????...?...???????????command?n???????done?????
示例-select.sh
[python]?view plaincopy
??????echo?"What's?your?favorate?OS?"????select?var?in?"Linux"?"Windows"?"UNIX"?"Other"??do??????break??done????echo?"You?have?selected?$var"??
四、case/esac
格式:
[python]?view plaincopy
case?變量?in???字符串1)???????命令列表1???????;;???????...???字符串n)???????命令列表n???????;;???esac??
示例-case.sh
[python]?view plaincopy
??????echo?"*********************************"??echo?"Please?select?a?oprator?as?below:"??echo?"C?...?copy"??echo?"D?...?delete"??echo?"B?...?backup"??echo?"*********************************"????read?op??case?$op?in??????C)????????????echo?"copy...."??????????;;????????D)????????????echo?"delete...."??????????;;????????B)????????????echo?"backup..."??????????;;??????*)??????????echo?"Unknow?operator!"??????????exit?1??esac??
示例-select.case
[python]?view plaincopy
??????echo?"a?is?5,?b?is?3,?please?select?your?method"??a=5??b=3;????select?var?in?"a+b"?"a-b"?"a*b"?"a/b"??do??????break??done????case?$var?in??????"a+b")??????????echo?"a+b="`expr?$a?+?$b?`??????????;;????????"a-b")??????????echo?"a-b="`expr?$a?-?$b`??????????;;????????"a*b")??????????echo?"a*b="`expr?$a?\*?$b`??????????;;??????"a/b")??????????echo?"a/b="`expr?$a?/?$b`??????????;;??????*)??????????echo?"input?error..."??????????exit?1??esac??
實例-/etc/rc.d/init.d/httpd部分源代碼
[python]?view plaincopy
??case?"$1"?in????start)??????start??????;;????stop)??????stop??????;;????status)??????;;????restart)??????stop??????start??????;;??????condrestart|try-restart)??????if?status?-p?${pidfile}?$httpd?>&/dev/null;?then??????????stop??????????start??????fi??????;;????force-reload|reload)??????????reload??????;;????graceful|help|configtest|fullstatus)??????$apachectl?$@??????RETVAL=$???????;;????*)??????echo?$"Usage:?$prog?{start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"??????RETVAL=2??esac??
五、while
格式:
[python]?view plaincopy
while?條件??????do???????命令???done???
示例-while.sh
[python]?view plaincopy
??????num=1??while?[?$num?-le?10?]??do??????echo?$(expr?$num?\*?$num)??????let?num++??done??
示例-useradd.sh
[python]?view plaincopy
????????echo?-n?"Plese?input?the?user?name:?"??read?username??echo?-n?"Plese?input?the?sum?users:?"??read?sum????num=1??while?[?$num?-le?$sum?]??do??????/usr/sbin/useradd?"$username$num"?????????if?[?$??-ne?0?]???????then??????????echo?"user:?$username?already?exists."??????????exit?1??????fi??????????????let?num++??done????echo?-n?"Please?input?the?passwd?for?this?users:?"??read?passwd????i=1??while?[?$i?-le?$sum?]??do??????echo?$passwd?|?/usr/bin/passwd?--stdin?"$username$i"??????let?i++??done??
示例-userdel.sh
[python]?view plaincopy
??????echo?-n?"Please?input?the?username:?"??read?username??echo?-n?"Please?input?the?user?number:?"??read?num?????i=1??while?[?$i?-le?$num?]??do??????/usr/sbin/userdel?-r?$username$i??????if?[?$??-ne?0?]???????then??????????echo?"User:?$username$i?is?not?exists."??????????let?i++???????????continue??????fi??????????let?i++???done??
六、until
格式:
[python]?view plaincopy
????until?條件???????do???????????命令???????done?????
示例-until.sh
[python]?view plaincopy
??????echo?"Please?input?Y/y?to?stop..."??read?input????until?[?"$input"?=?"Y"?]?||?[?"$input"?=?"y"?]??do??????echo?"input?error,?input?again!"??????read?input??done??
七、跳出循環:break和continue?
break:跳出整個循環?
continue:跳過本次循環,進行下次循環
?
示例-break_continue.sh
[python]?view plaincopy
??????while?true??do??????echo?"*****************************"??????echo?"Please?have?a?select?as?blow:"??????echo?"1?Copy"??????echo?"2?Delete"??????echo?"3?Backup"??????echo?"4?Quit***********************"??????read?op????????case?$op?in??????????"1")??????????????echo?"$op?is?Copy"??????????????;;????????????"2")??????????????echo?"$op?is?Delete"??????????????;;????????????"3")??????????????echo?"$op?is?Backup"??????????????;;??????????"4")??????????????echo?"Exit..."??????????????break??????????????;;??????????"*")??????????????echo?"Invalide?selectino,?please?select?again..."??????????????continue??????????????;;??????esac??done??
?
八、shift指令
參數左移,每執行一次,參數序列順次左移一個位置,$#的值減1,?用于分別處理每個參數,移出去的參數不再可用
?
示例-shift.sh
[python]?view plaincopy
??????if?[?$??then??????echo?"No?enough?parameters"??????exit?1??fi????num=0??while?[?$??do??????echo?'$1?is?'$1??????let?num++??????shift??done????echo?$num??
函數應用
實例-/etc/rc.d/init.d/httpd中的start源代碼
?
一、函數的定義:?
[python]?view plaincopy
函數名?()???{???????命令序列???}???
二、函數的調用:不帶()?
函數名?參數1?參數2?...?參數n
實例-調用
?
?
三、函數中的變量:?
變量均為全局變量,沒有局部變量
?
四、函數中的參數:
調用函數時,可以傳遞參數,在函數中用$1、$2...來引用?
?
示例-function.sh
[python]?view plaincopy
????????Help(){??????echo?"Usage:?sh?function?\$1?\$2?\$3"??}????Display(){??????echo?"three?argument:?$1?$2?$3"??}??????if?[?$??then??????Help??else??????echo?"Think?you?for?your?input"??????Display?$1?$2?$3??fi??
Shell?腳本調試?
sh?-x?script? 這將執行該腳本并顯示所有變量的值。?
sh?-n?script? 不執行腳本只是檢查語法的模式,將返回所有語法錯誤。?
?
最佳實踐-命令最好使用絕對路徑
?
一個腳本能夠執行-
1.對腳本有rx權限,只有r,可以使用sh執行
2.對腳本所在目錄至少有rx權限
?
拓展實例-setuid.sh
[python]?view plaincopy
??????????/bin/find?/?-perm?-4000?-o?-perm?-2000?>?/tmp/setuid.list?2>?/dev/null????for?var?in?`/bin/cat?/tmp/setuid.list`??do??????/bin/grep?$var?/backup/setuid.list?>?/dev/null?2>?/dev/null??????if?[?$??-ne?0?]???????then??????????echo?"$var?is?not?in?/backup/setuid.list,?It's?danger!"??????fi????done ?
總結
以上是生活随笔為你收集整理的Shell编程入门(第二版)(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。