日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linuxshell之结构化命令

發(fā)布時間:2025/3/21 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linuxshell之结构化命令 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用if-then語句
如果命令的退出狀態(tài)是0(成功執(zhí)行命令),將執(zhí)行then后面的所有命令。
如果命令的退出狀態(tài)是0以外的其他值,那么then后面的命令將不會執(zhí)行,bash shell會移動到腳本的下一條命令。

#!/bin/bash # testing the if statement if date thenecho "it worked" fi

(date返回0,執(zhí)行then語句it worked)

#!/bin/bash #testing multiple commands in the then section testuser=jiqing9006 if grep $testuser /etc/passwd thenecho The bash files for user $testuser are:ls -a /home/$testuser/.b* fi

(The bash files for user jiqing9006 are:
/home/jiqing9006/.bash_logout? /home/jiqing9006/.bash_profile? /home/jiqing9006/.bashrc
if語句行使用grep命令搜索/etc/passwd文件,查看系統(tǒng)是否正在使用一個特定的用戶名。
如果一個用戶擁有該登錄名,腳本會顯示一些文本,然后列出用戶HOME目錄下的bash文件


if-then-else語句

#!/bin/bash #testing multiple commands in the then section testuser=jim if grep $testuser /etc/passwd thenecho The bash files for user $testuser are:ls -a /home/$testuser/.b* elseecho "The user name $testuser does't exist on this system" fi


(如果不存在,執(zhí)行下面的語句)

嵌套if語句

#!/bin/bash #testing multiple commands in the then section user1=jim user2=jiqing9006 if grep $user1 /etc/passwd thenecho The bash files for user $user1 are:ls -a /home/$user1/.b* elif grep $user2 /etc/passwd thenecho The bash files for user $user2 are:ls -a /home/$user2/.b* elseecho "The user name $user1 and $user2 does't exist on this system" fi

(不是elseif 而是elif ,注意與其它語言的區(qū)別)

test命令
if [condition]
then?
? commands
?fi
?test命令能夠評估三類條件
?數(shù)值
?字符串
?文件
?a.數(shù)值比較
?n1 -eq n2(是否等于)
?n1 -ge n2(是否大于等于)
?n1 -gt n2(是否大于)
?n1 -le n2(是否小于等于)
?n1 -lt n2(是否小于)
?n1 -ne n2(不等于)

#!/bin/bash #using numeric test comparisons val1=10 val2=11 if [ $val1 -gt 5 ] thenecho "The test value $val1 is greater than 5" fiif [ $val1 -eq $val2 ] thenecho "The values are equal" elseecho "The values are not equal" fi

(注意if與[之間有空格,[與$val1之間有空格)
test命令無法處理浮點數(shù),bash shell只能處理整數(shù)數(shù)字

b.字符串比較
str1 = str2 (str1與str2是否相同)
str1 != str2 (是否不同)
str1 < str2(是否小于)
str1 > str2 (是否大于)
-n str1(長度是否大于0)
-z str1(長度是否為0)

#!/bin/bash #testing string equality testuser=root if [ $USER = $testuser ] thenecho "Welcome $testuser" fi

(比較相等)

#!/bin/bash #testing string equality testuser=baduser if [ $USER != $testuser ] thenecho "This isn't $testuser" elseecho "Welcome $testuser" fi

(不相等比較,所有標(biāo)點符號和大小寫都考慮在內(nèi))

使用大小于號,需要轉(zhuǎn)義一下

#!/bin/bash #mis-using string comparisons val1=baseball val2=hockey val3=book val4=Book if [ $val1 \> $val2 ] thenecho "$val1 is greater than $val2" elseecho "$val1 is less than $val2" fi if [ $val1 \> $val3 ] thenecho "$val1 is greater than $val3" elseecho "$val1 is less than $val3" fi if [ $val1 \> $val2 ] thenecho "$val3 is greater than $val4" elseecho "$val3 is less than $val4" fi

結(jié)果:
baseball is less than hockey
baseball is less than book
book is greater than Book
(test命令采用的是ascii碼排序的,sort采用的是當(dāng)前語言設(shè)置定義的排列順序。由結(jié)果可知,比較第一個字母b小于h,如果第一個字符相同,比較第二個字符,a小于o。小寫字符,大于大寫字符a97,A65。)

ls -lt(按時間排序,最新的在最前面)
ls -l|sort -k1
ls -l|sort -k2
ls -l|sort -k3(按照第幾列進行排序)
..

#!/bin/bash #testing string length str1="helloworld" str2="" if [ -n $str1 ] thenecho "str1 is not null" elseecho "str1 is null" fiif [ -z $str2 ] thenecho "str2 is null" elseecho "str2 is not null" fi

(我自己寫的代碼,發(fā)現(xiàn)str中不能有空格,否則無法判斷)

c.文件比較
test命令能夠測試Linux文件系統(tǒng)上的文件狀態(tài)和路徑。
-d file 檢查file是否存在并且是一個目錄
-e file 檢查file是否存在
-f file 檢查file是否存在并且是一個文件
-r file 檢查file是否存在并且可讀
-s file 檢查file是否存在并且不為空
-w file 檢查file是否存在并且可寫
-x file 檢查file是否存在并且可執(zhí)行
-O file 檢查file是否存在并且被當(dāng)前用戶擁有
-G file 檢查file是否存在并且被當(dāng)前組擁有
file1 -nt file2 檢查file1是否比file2新
file2 -ot file2 檢查file1是否比file2舊
如果想將文件寫到一個目錄下,或試圖改變到目錄位置之前,最好檢查一下-d

#!/bin/bash #look before you leap if [ -d $HOME ] thenecho "Your HOME directory exists"cd $HOMEls -l elseecho "There is a problem with your HOME directory" fi

(檢查一個目錄是否存在,存在就做一些事情,不存在就提示錯誤。)
在腳本中使用文件或目錄之前,-e比較能夠檢查它們是否存在

#!/bin/bash # check if a directory exists if [ -e $HOME ] thenecho "Your home directory exists"#check if file exists in the directoryif [ -e $HOME/testing ]thenecho "$HOME/tesing exist in the directory"date >>$HOME/testingelseecho "Create a new file"date >$HOME/testingfi elseecho "Your home directory doesn't exists" fi

(這里稍微復(fù)雜了一點,用了嵌套,并且注釋很清晰,提示很到位)
-f檢測文件

#!/bin/bash #testing if a file if [ -e $HOME ] thenecho "The obj exist,if it is a file?"if [ -f $HOME ]thenecho "Yes,it is a file!"elseecho "No,it is not a file!"if [ -f $HOME/testing ]thenecho "But $HOME/testing is a file!"elseecho "$HOME/testing is not a file too!"fifi elseecho "The obj doesn't exist" fi

(這個好無聊,不過展示了一個流程,逐級向下進行查詢)
是否能讀
在嘗試從文件中讀取數(shù)據(jù)之前,通常首先檢查一下是否能讀取文件。-r

#!/bin/bash #testing if can read a file pwfile=/etc/shadow #first,check if it is a file if [ -f $pwfile ] then#now test if you can read itif [ -r $pwfile ]thentail $pwfileelseecho "sorry,the file can be read."fi elseecho "sorry,it is not a file." fi

(注意變量使用是一定要加上$符,不然就會出錯)

檢查空文件
應(yīng)該用-s比較來檢查文件是否為空,尤其是想刪除文件時。注意,-s比較成功時,它表明文件包含數(shù)據(jù)

#!/bin/bash #testing a file is empty file=testfile touch $fileif [ -s $file ] then echo "The $file exists and has data in it" elseecho "The $file doesn't exist or is empty" fidate >$file if [ -s $file ] thenecho "The $file exists and has data in it" elseecho "The $file doesn't exist or is empty" fi

(真表示有數(shù)據(jù),假表示無數(shù)據(jù))

檢查是否能夠?qū)懭霐?shù)據(jù)-w

#!/bin/bash #checking if a file if writeable logfile=$HOME/logtest touch $logfile chmod u-w $logfile now=`date +%Y%m%d-%H%M`if [ -w $logfile ] thenecho "The program ran at:$now">>$logfileecho "The first attempt succeeded" elseecho "The first attempt failed" fichmod u+w $logfile if [ -w $logfile ] thenecho "The program ran at:$now">>$logfileecho "The second attempt succeeded" elseecho "The second attempt failed" fi

(這里的判斷有點問題,需要再研究)

..
檢查文件日期

#!/bin/bash #testing file dates if [ ./test1 -nt ./test10 ] thenecho "The test1 file is newer than test10" elseecho "The test10 file is newer than test1" fiif [ ./test1 -ot ./test10 ] thenecho "The test1 file is older than test10" elseecho "The test10 file is older than test1" fi

(比較兩個文件的新舊)

復(fù)合條件檢查
&&
||

#!/bin/bash #testing compound comparisons if [ -d $HOME ] && [ -x $HOME/testing ] thenecho "The file exists and you can execute it" elseecho "You can't execute the file" fi

(并列執(zhí)行)

if-then的高級特征
(())雙圓括號,可以進行復(fù)雜的算術(shù)操作
val++ 后增量
val-- 后減量
++val 前增量
--val 前減量
!???? 邏輯否定
~???? 取反
**??? 取冪
<<??? 左移
>>??? 右移
&
|
&&??? 邏輯與
||??? 邏輯或

#!/bin/bash #using double parenthesis val1=10 if (($val1**2>90)) then((val2 =$val1**2))echo "The square of $val1 is $val2" fi

(雙圓括號里面的內(nèi)容更加智能,會識別很多東西,不用總是空格空格的編寫代碼了)

[[]]雙方括號
可以進行字符串比較,更加智能
模式匹配,也就是正則表達(dá)式可以更好的使用

#!/bin/bash # using pattern matching if [[ $USER==r* ]] thenecho "Hello $USER" elseecho "Sorry,I don't know you" fi

(規(guī)范編寫)

case使用

#!/bin/bash #using the case command case $USER in rich | barbara)echo "Welcome,$USER"ehco "Please enjoy your visit";; testing)echo "Special testing account";; jessica)echo "Don't forget to log off when you're done";; root)echo "Welcome,Manager";; *)echo "Sorry,you're not allowed here";; esac


本文轉(zhuǎn)自TBHacker博客園博客,原文鏈接:http://www.cnblogs.com/jiqing9006/p/3198346.html,如需轉(zhuǎn)載請自行聯(lián)系原作者

總結(jié)

以上是生活随笔為你收集整理的Linuxshell之结构化命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。