linux shell指令碼學習筆記

2021-10-09 09:40:00 字數 4167 閱讀 4669

if command

then

commands

fi

if後面的命令,執行後狀態碼返回0,則執行then部分的命令

#執行pwd

wang@wang:~/test$ pwd

/home/wang/test

#檢視退出碼

wang@wang:~/test$ echo $?

0#執行pwda

wang@wang:~/test$ pwda

未找到 'pwda' 命令,您要輸入的是否是:

命令 'pwdx' 來自於包 'procps' (main)

命令 'pwd' 來自於包 'coreutils' (main)

命令 'pida' 來自於包 'pida' (universe)

命令 'pda' 來自於包 'speech-tools' (universe)

pwda:未找到命令

#檢視退出碼

wang@wang:~/test$ echo $?

127#pwd執行成功,退出碼為0,而pwda命令執行失敗,退出碼非0

if pwd

then

echo "then"

else

echo "else"

fi#執行結果

wang@wang:~/test$ ./a.sh

/home/wang/test

then

#pwd命令執行成功,退出碼為0,執行then語句塊

#!/bin/bash

val="test"

if val

then

echo "then"

else

echo "else"

fiwang@wang:~/test$ ./a.sh

./a.sh: 行 5: val: 未找到命令

else

#執行"if val"時,提示未找到命令,退出碼非0,指令碼執行then語句塊

如果判斷val是否有值,可以使用test命令,語法如下:

if test condition

then

commands

fi

val="hello"

if test $val

then

echo "return true"

else

echo "return false"

fi#執行結果

wang@wang:~/test$ ./a.sh

return true

bash shell 提供了另一種測試方法,無需宣告test命令

if [ condition ]

then

command

fi

wang@wang:~/test$ cat a.sh 

#!/bin/bash

val="hello"

if [ $val ]

then

echo "return true"

else

echo "return false"

fiwang@wang:~/test$ ./a.sh

return true

n1 -eq n2	檢查n1是否等於n2

n1 -ge n2 檢查n1是否大於等於n2

n1 -gt n2 檢查n1是否大於n2

n1 -le n2 檢查n1是否小於等於n2

n1 -lt n2 檢查n1是否小於n2

n1 -ne n2 檢查n1是否不等於n2

value1=10

value2=11

if [ $value1 -lt $value2 ]

then

echo "$value1 is less then $value2"

fi#執行結果

wang@wang:~/test$ ./a.sh

10 is less then 11

str1 = str2	檢查str1是否等於str2

str1 != str2 檢查str1是否不等於str2

str1 < str2 檢查str1是否小於str2

str1 > str2 檢查str1是否大於str2

-n str1 檢查str1長度是否非0

-z str1 檢查str1長度是否為0

str1=hello

str2=world

if [ $str1 \< $str2 ]

then

echo "$str1 is less then $str2"

fi#執行結果

wang@wang:~/test$ ./a.sh

hello is less then world

注意:

使用大於,小於符號時需要轉義

-d file		檢查file是否存在並是乙個目錄

-e file 檢查file是否存在

-f file 檢查file是否存在並是乙個檔案

-r file 檢查file是否存在並可讀

-s file 檢查file是否存在並非空

-w file 檢查file是否存在並可寫

-x file 檢查file是否存在並可執行

-o file 檢查file是否存在並屬當前使用者

-g file 檢查file是否存在且預設組與當前使用者相同

f1 -nt f2 檢查f1是否比f2新

f1 -ot f2 檢查f1是否比f2舊

location=$home

if [ -e $location ]

then

echo "$location exists"

fi#執行結果

wang@wang:~/test$ ./a.sh

/home/wang exists

[ condition1 ] && [ condition2 ]

[ condition1 ] || [ condition2 ]

if [ -d $home ] && [ -w $home/test.sh ]

then

echo "$home/test.sh exists and you can write to it"

fi

(( expression ))
val1=10

if (( $val1 ** 2 > 80 ))

then

(( val2 = $val1 ** 2 ))

echo "val2 is $val2"

fi#執行結果

wang@wang:~/test$ ./a.sh

val2 is 100

[[ expression ]]
echo $user

if [[ $user == w* ]]

then

echo "hello"

fi#執行結果

wang@wang:~/test$ ./a.sh

wang

hello

case variable in

pattern1 | pattern2)

command1;;

pattern3)

command2;;

*) command3;;

esac

case $user in 

wangm | han)

echo "welcome $user";;

testing)

echo "testing account";;

*) echo "sorry, you are not allowed here";;

esac

Linux Shell指令碼學習(一)

最近花了一周的時間快速的學習了一下shell指令碼,看的書是 linux shell指令碼程式設計 第三版 僅僅整理了一些筆記,不做技術交流也不算分享,只是作為自己的學習之路的記錄,務戀。shell one 開始學習linux shell指令碼,首日了解了linux命令列 linux shell終端...

linux shell 指令碼攻略第3版 讀書筆記

第2章 1shell會擴充套件沒有引號或是出現在雙引號 中 的萬用字元。單引號能夠阻止shell擴充套件 txt,使得該字串能夠原封不動地傳給find命令。2find 命令 如果需要用到正規表示式使用單引號 例如 查詢 home slynux下面 所有以txt結尾的檔案並列印出來 find home...

Linux shell指令碼全面學習 一

1.linux 指令碼編寫基礎 1.1 語法基本介紹 1.1.1 開頭 程式必須以下面的行開始 必須方在檔案的第一行 bin sh 符號 用來告訴系統它後面的引數是用來執行該檔案的程式。在這個例子中我們使用 bin sh來執行程式。當編輯好指令碼時,如果要執行該指令碼,還必須使其可執行。要使指令碼可...