Shell腳本基礎語法

一、       編寫shell的格式

1 第一行決定使用哪一種shell

Bash:#! /bin/bash

Python: #! /usr/bin/python

不知道解釋器的完整路徑,可使用whereis 解釋器名稱(bash)

2 # …… 表示註釋

3 正常的程序體

 

二、       source . bash sh ./ 執行腳本有啥區別

1 source xxx.sh 等價於 . xxx.sh: 他們是在父進程中執行的,因此各項操作都會在原本的bash環境下生效。所以通過這兩種執行的shell 腳本內的變量,在shell腳本執行完畢之後,也是可以查詢的。

說白了,source主要是加載環境變量的。

2 ./ sh 以及 bashxxx.sh :他們是父進程會新開一個子進程,然後,這時候bash環境已經是一個新的bash環境,當子進程結束後,子進程內的變量或操作不會回傳到父進程。而且sh就是bash:

[root@coolpay10 ~]# ls -al /usr/bin/sh

lrwxrwxrwx. 1 root root 4 Dec 30 19:25 /usr/bin/sh -> bash

 

三、       變量和引用

3.1變量運算符

3.1.1 替換運算符

${var:-word} var存在且非空,返回var值,否則返回word

${var:+word} 對已賦值的var,重設其值

${var:=word} var存在且非空,返回var值,否則將var的值賦值爲word,然後返回這個值

${var:?word} 如果var存在切非空,返回var值,否則打印

word內容。

[root@localhost nicky]# echo ${endeca?'unknownvariable'}

-bash: endeca: unknown variable

[root@localhost nicky]# echo ${endeca:?'unknownvariable'}

-bash: endeca: unknown variable

[root@localhost nicky]# endeca="Poweful SearchEngine Framework"

[root@localhost nicky]# echo ${endeca:?'unknownvariable'}

Poweful Search Engine Framework

root@localhost nicky]# echo ${endeca:='unknownvariable'}

Poweful Search Engine Framework

[root@localhost nicky]# echo ${endeca1:='unknownvariable'}

unknown variable

[root@localhost nicky]# echo ${endeca1:-'unknownvariable'}

unknown variable

[root@localhost nicky]# echo ${endeca2:-'unknownvariable'}

unknown variable

3.1.2 模式匹配運算符

變量運算符

${var#pattern}:

如果模式匹配變量的開始處,則刪除匹配最短部分

echo ${var#/*}

結果爲:opt/endeca/log/file.log

echo ${var#/opt}

結果爲:/endeca/log/file.log

 

${var##pattern}:

如果模式匹配變量的開始處,則刪除匹配最長部分

echo ${var##/opt}

結果爲:/endeca/log/file.log

echo ${var##/*/}

結果爲:file.log

 

${var%pattern}

如果模式匹配變量的結尾處,則刪除匹配最短部分

echo ${var%*.log}

結果爲:/opt/endeca/log/file

 

${var%%pattern}:如果模式匹配變量的結尾處,則刪除匹配最長部分

 

${var/pattern/string}

${var//pattern/string}

1匹配模式最長的部分替換爲string

2匹配模式所有匹配部分替換爲string

echo -e ${path//:/'\n'}所有:替換成換行

3.2 變量類型

3.2.1用戶變量

用戶變量:在shell 腳本定義的變量,分爲全局變量和本地變量

默認情況下,都是全局變量,除非顯示聲明類型是local的,纔是局部變量。

語法:varname=value

注意:正確的賦值和變量替換方式

正確:[root@localhostnicky]# str="how are you"

正確:[root@localhostnicky]# str=hello

錯誤:[root@localhostnicky]# str=hello world

錯誤:[root@localhostnicky]# str = hello

錯誤:[root@localhostnicky]# str = "how are you"

報錯:-bash: are:command not found

原因:賦值可以不加引號,但是隻能是沒有空格的字符;如果有空格必須,用雙引號引起來;而且=兩邊不能有空格

刪除變量:unset 變量名稱

3.2.2 位置變量

位置變量即所謂的位置參數:

$0: 命令名稱

$*: 所有的位置參數,不包括$0,因爲$0表示執行命令

$#: 位置參數的個數

$*: 所有位置變量上的內容

$?: 上一條命令執行後的返回狀態,如果是0表示正常,如果不是表示你出現異常或者錯誤

!$: 上一條命令的最後一個參數

 

3.2.3 環境變量

環境變量的值適用於所有由登錄進程所產生的子進程,定義環境變量的基本格式:

ENVRION-VARIABLE=VALUE #賦值

Export ENVRION-VARIABLE #聲明環境變量

四、       測試 退出 判斷 操作符

4.1 測試

語法格式:test expression

4.1.1整數比較運算符:

-eq: 等於

-ge: 大於或者等於

-le: 小於或者等於

-gt: 大於

-lt: 小於

-ne: 不等於

4.1.2 字符串比較運算符:

String: string 是否不爲空

-n string: string 是否不爲空

-z string: string 是否爲空

Str1=Str2: str1 是否等於str2

Str1!=Str2: str1 是否不等於str2

4.1.3 文件操作符:

-e file: 文件是否存在

-d file: 文件是否爲目錄

-f file: 文件是否爲普通文件

-r: 文件是否可讀

-w: 文件是否可寫

-x: 文件是否可操作

-h|-L: 文件是否是連接文件

-s: 該文件是否存在,且爲非空白文件

-S: 是否爲Socket文件

-nt: 前面的文件是否比後面的文件新

-ot: 前面的文件是否比後面的文件舊

4.1.4 邏輯運算符:

!exp: 是否爲false

-a: 表示且

-o: 表示或

4.2 判斷

4.2.1 if 判斷

if(exp1) then

      statement……

elif(exp2) then

      statement……

else

      statement……

fi

4.2.2 exit

語法:exit status

Status: 0-255 之間的數字表示,一般返回該狀態的值的同時伴隨着腳本的退出,參數被保存到shell變量$?中。

#! /bin/bash

echo -e "input a string:"

read str1

if [ -z "$str1" ] # ; then

then

    echo "what youinput is null!";

    exit 1;

fi

4.2.3 case

case variable in

var1)

……command……

;;

var2)

……command……

;;

*)

……command……

;;

Esac

 

五、循環與結構化變量

5.1 for循環

5.1.1 列表for循環

for variable in {list}

do

      statements......

done

5.1.1 不帶列表for循環,由用戶指定參數和參數個數

for variable

do

      statements......

done

 

5.1.1 c類for循環

for ((exp1; exp2; exp3))

do

      statements......

done

5.2 while 循環

5.2.1 計數器控制的while循環

int x=5;

while (("$int" <=  5))

do

      echo"$int"

      let"$int++"

done

5.2.1 結束標記控制的while循環

echo "please input the num[1-10]:"

read num

while[[ "$num" != 4 ]]

do

      if [ "$num"-lt 4 ]; then

           echo"Too Small, Try Again"

           read num

      elif ["$num" -gt 4 ]; then

           echo"Too High, Try Again"

           read num

      else

           exit 0

      fi

      echo "You arehit the results!"

done

5.2.1 標誌控制的while循環

echo "please input the num[1-10]:"

read num

signal=0

while[[ "signal" != 1 ]]

do

      if ["$num" -lt 4 ]; then

           echo"Too Small, Try Again"

           read num

      elif ["$num" -gt 4 ]; then

           echo"Too High, Try Again"

           read num

      else

           signal=1

           exit 0

      fi

      echo "You arehit the results!"

done

5.2.1 命令行控制的while循環

5.3.until循環,直到滿足什麼才做某事

i=0

until [[ "$i" -gt 10 ]]

do

      let"s=i*i"

      echo "$i * $i= $s"

      let"i++"

done

 

5.4 select 結構

[root@localhost nicky]# cat > 04.sh <<"end"

> #! /bin/bash

> echo "what is you like color?"

> select color in "red" "blue" "black""green" "orange" "others"

> do

>     break

> done

> echo -e "you have selected $color"

> end

[root@localhost nicky]# chmod 777 04.sh

[root@localhost nicky]# ./04.sh

what is you like color?

1) red

2) blue

3) black

4) green

5) orange

6) others

#? 3

you have selected black

[root@localhost nicky]#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章