開始學(xué)習(xí)吧:
Shell 語法 :
1.變量
Linux是區(qū)分大小寫的系統(tǒng);使用變量名前面加上$來訪問變量的內(nèi)容,為變量賦值時(shí),只是需要變量名,變量會(huì)根據(jù)需要自動(dòng)創(chuàng)建
$salutation=hello$echo $salutationhello$salutation="YesDear"$echo $salutationYesDear1.1 read 命令等待用戶輸入并且賦值
$read salutationWiegeht's$echo $salutationWiegeht's1.2 引號(hào)的使用雙引號(hào)不會(huì)影響變量的替換,單引號(hào)和反斜杠不進(jìn)行變量轉(zhuǎn)換
$varTest="Test"$echo $varTestTest$echo"$varTest"Testg$echo '$varTest'$varTestecho /$varTest$varTest1.3環(huán)境變量$0:shell腳本的名字
$$腳本的進(jìn)程號(hào),腳本通常使用它生成一個(gè)臨時(shí)文件eg:/tmp/tmpfile_$$
$#傳給給腳本的參數(shù)個(gè)數(shù)
2.條件
test或[命令
eg1:檢查一個(gè)文件是否存在
if test -f fred.cthen....fiorif[ test -f fred.c]; then....fips:[ A ]和檢查內(nèi)容A之間需要空格,如果想if和then同一行,那么需要分號(hào)2.1字符串比較str1 = str2 相等為真str2 != str2 不相等為真-n str1 不為空為真-z str1 為空為真2.2數(shù)字比較A -eq B 相等為真A -ne B 不相等為真A -gt B A>BA -ge B A>=BA -lt B A<BA -le B A<=B2.3文件條件測(cè)試-d file 為目錄為真-e file 存在為真,盡量使用-f-f file 存在為真-g file 如果文件的set-group-id位被設(shè)置則為真-r file 可讀為真-s file 大小不為0為真-u file 如果文件的set-user-id位被設(shè)置則為真-w file可寫為真-x file可執(zhí)行為真2.4控制結(jié)構(gòu)(1).if語句 if condition then statements else statements fi(2).elif語句
#!/bin/shecho"Ii't morning? Please anser yes or no"read timeofdayif[ "$timeofday"="yes" ] then echo"Goodmorning"elif [ "$timeofday"="no" ];then echo"Goodafternoon"else echo "Sorry,$timeofday not recognized,Enter yes or no" exit1fiexit0本次語法筆記結(jié)束,下次繼續(xù)shell語法(2)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注