linux shell腳本

定義:

linux shell script是一組shell命令組成的批處理文件。相似於windows的bat文件。 linux

shell腳本基本語法

  • 每當讀取到一個ENTER符號,就開始嘗試執行該行的命令。
  • 若是一行的命令太長,能夠在行尾使用反斜槓\將命令延續到下一行。

[root@localhost testsh]# cat testenter.sh shell

echo zjf is a good\ express

manvim

  • #開頭表明註釋。

腳本的執行

腳本的執行方式在前幾篇文章裏有敘述。這裏要重點關注這點: windows

sh和./的方式,須要的權限不同。sh只須要接受一個參數,因此可讀就能夠,可是./須要執行權限。 bash

[root@localhost testsh]# chmod 666 testenter.sh oop

[root@localhost testsh]# sh testenter.sh spa

zjf is a goodman 操作系統

[root@localhost testsh]# ./testenter.sh .net

-bash: ./testenter.sh: 權限不夠

數值運算

bash的數值運算能夠執行簡單的加減乘除,有兩種方式:

  • 直接進行數值運算

這種方式,須要讓解釋器知道當前處理的變量是數值型的。不然將不會進行數值運算。

以下:

錯誤的方式:

[root@localhost testsh]# x=10;

[root@localhost testsh]# y=2

[root@localhost testsh]# z=x*y

[root@localhost testsh]# echo $z

x*y

及時x和y是數值型的也不行:

[root@localhost testsh]# x=10

[root@localhost testsh]# declare -i y=2

[root@localhost testsh]# z=x*y

[root@localhost testsh]# echo $z

x*y

必須聲明z爲數值型:

[root@localhost testsh]# x=10;

[root@localhost testsh]# y=2

[root@localhost testsh]# declare -i z

[root@localhost testsh]# z=x*y

[root@localhost testsh]# echo $z

20

  • 使用$((運算內容)),不須要聲明數值類型。

注意,這裏是兩個括號。

[root@localhost testsh]# x=10

[root@localhost testsh]# y=2

[root@localhost testsh]# z=$((x*y))

[root@localhost testsh]# echo $z

20

 

條件邏輯

  • 第一種方式,使用test命令。

test命令不返回true或者false結果,因此使用以下方式是達不到效果的。

if [ $(test -e /etc) ]; then

echo 111

fi

雖然不會報錯,可是if條件是不會知足的。

能夠經過$?獲取test的返回狀態碼,來判斷test成功或者失敗。

[root@localhost testsh]# test -e /etc

[root@localhost testsh]# echo $?

0

[root@localhost testsh]# test -e /etc1

[root@localhost testsh]# echo $?

1

正確的使用方式1:

[root@localhost testsh]# test -e /etc && echo "exists" || echo "not exists"

exists

其中,&&後面跟着執行test成功後執行的語句,||後面跟着test失敗後執行的語句。

正確的使用方式2:

if test -e /etc

then

echo 111

fi

 

test命令明細,沒有必要所有記下來。可是要知道能夠作什麼:

判斷字符串

test –n 字符串                                   字符串的長度非零

  test –z 字符串                                    字符串的長度爲零

  test 字符串1=字符串2                    字符串相等

  test 字符串1!=字符串2               字符串不等

判斷整數

  test 整數1 –eq 整數2                        整數相等

  test 整數1 –ge 整數2                        整數1大於等於整數2

  test 整數1 –gt 整數2                         整數1大於整數2

  test 整數1 –le 整數2                         整數1小於等於整數2

  test 整數1 –lt 整數2                          整數1小於整數2

  test 整數1 –ne 整數2                        整數1不等於整數2

判斷文件

  test  File1 –ef  File2                            兩個文件具備一樣的設備號和i結點號

  test  File1 –nt  File2                            文件1比文件2 新

  test  File1 –ot  File2                            文件1比文件2 舊

  test –b File                                           文件存在而且是塊設備文件

  test –c File                                           文件存在而且是字符設備文件

  test –d File                                           文件存在而且是目錄

  test –e File                                           文件存在

  test –f File                                            文件存在而且是正規文件

  test –g File                                           文件存在而且是設置了組ID

  test –G File                                           文件存在而且屬於有效組ID

  test –h File                                           文件存在而且是一個符號連接(同-L)

  test –k File                                           文件存在而且設置了sticky位

  test –b File                                           文件存在而且是塊設備文件

  test –L File                                           文件存在而且是一個符號連接(同-h)

  test –o File                                           文件存在而且屬於有效用戶ID

  test –p File                                           文件存在而且是一個命名管道

  test –r File                                            文件存在而且可讀

  test –s File                                           文件存在而且是一個套接字

  test –t FD                                             文件描述符是在一個終端打開的

  test –u File                                           文件存在而且設置了它的set-user-id位

  test –w File                                          文件存在而且可寫

  test –x File                                           文件存在而且可執行

多重條件判斷:

  • if test表達式 #表達式爲真
  • if test ! 表達式 #表達式爲假 注意!先後有空格
  • test 表達式1 –a 表達式2 #兩個表達式都爲真 a表明and
  • test 表達式1 –o 表達式2 #兩個表達式有一個爲真 o表明or

[root@localhost testsh]# if test 1==1 ;then echo 111;fi;

111

[root@localhost testsh]# if test ! 1==1 ;then echo 111;fi;

 

[root@localhost testsh]# if test 1==1 -a -e /etc ;then echo 111;fi;

111

[root@localhost testsh]# if test 1==2 -o -e /etc ;then echo 111;fi;

111

 

  • 第二種方式,使用[]

 雖然 Linux 和 UNIX 的每一個版本中都包含 test 命令,但該命令有一個更經常使用的別名 — 左方括號:[。test 及其別名一般均可以在 /usr/bin 或 /bin (取決於操做系統版本和供應商)中找到。當您使用左方括號而非 test 時,其後必須始終跟着一個空格、要評估的條件、一個空格和右方括號。右方括號不是任何東西的別名,而是表示所需評估參數的結束。條件兩邊的空格是必需的,這表示要調用 test,以區別於一樣常常使用方括號的字符/模式匹配操做。

test 和 [ 的語法以下:

  test expression

  [ expression ]

[root@localhost testsh]# if [ "111"=="111" ]; then echo 111; fi;

111

[root@localhost testsh]# if [ -e /etc ]; then echo 111; fi;

111

字符串的比較

對字符串的比較,若是是變量必定要用""括起來

[root@localhost testsh]# if [ $name=="zjf man" ] ; then echo 111;fi;

-bash: [: zjf: 期待一元表達式

[root@localhost testsh]# if [ "$name"=="zjf man" ] ; then echo 111;fi;

111

由於第一個命令其實變成了if [zjf man=="zjf man" ] ; then echo 111;fi;

判斷爲空的兩種方式:

[root@localhost testsh]# unset name

[root@localhost testsh]# if [ "$name"=="" ] ; then echo 111;fi;

111

[root@localhost testsh]# if [ -z "$name" ] ; then echo 111;fi;

111

 

if判斷

 

if空格[空格條件判斷空格 ] ; then

    command

elif空格[空格條件判斷空格 ] ; then

    command

else

    command

fi

其中,elif和else是可選的。

&&表明and。

||表明or。

[ "$i"=="1" -o "$j"=="1" ] 等於 [ "$i"=="1" ] || [ "$j"=="1" ]

[ "$i"=="1" -a "$j"=="1" ] 等於 [ "$i"=="1" ] && [ "$j"=="1" ]

function功能

[root@localhost testsh]# vim testfunction.sh

 

function print()

{

echo "$1"

}

print 11

print "zjf"

 

[root@localhost testsh]# sh testfunction.sh

11

zjf

 

注意,定義的時候,括號內不能傳遞參數,參數是經過$1 $2來傳遞的。$0不是參數,是sh文件的名稱。

調用方法的時候,不是經過括號傳遞參數的方式,而是和調用命令相同的方式。

 

loop循環

 

while空格[空格條件空格]

do

    command

done

 

util空格[空格條件空格]

do

    command

done

[root@localhost testsh]# vim testwhile.sh

 

declare -i i=0

declare -i sum=0

while [ $i -lt 100 ]

do

sum=sum+i

i=i+1

done

echo $sum

 

[root@localhost testsh]# sh testwhile.sh

4950

 

for in循環

 

[root@localhost testsh]# vim testfor.sh

 

users=$(cut -d ':' -f1 /etc/passwd)

for username in $users

do

echo "i hava a user named $username"

done

 

[root@localhost testsh]# sh testfor.sh

i hava a user named root

i hava a user named bin

i hava a user named daemon

i hava a user named adm

 

for常見用法:

for i in $(seq 10) do 至關於for((i=1;i<=10;i++));

for i in f1 f2 f3 ;do

for i in $(ls)

for循環

注意有兩個括號。

for((i=1;i<10000000;i++));do

echo $i
done

相關文章
相關標籤/搜索