1. 程式人生 > >Shell 指令碼語言基本知識

Shell 指令碼語言基本知識

Shell的帖子

http://www.w2bc.com/article/223184

1. shell指令碼的開頭往往有一句話來定義使用哪種sh直譯器來解釋指令碼。目前研發送測的shell指令碼中主要有以下兩種方式:(1) #!/bin/sh(2) #!/bin/bash

shbash的區別,實際上是bash有沒開啟posix模式的區別。遵守posix規範,可能包括,當某行程式碼出錯時,不繼續往下執行。

2. echo命令,代表向視窗輸出文字

3. cd `dirname $0`       $0代表指令碼檔案     dirname $0表示獲得執行的當前目錄      表示切換到指令碼所在的目錄

4. 

shell檔案中,每個變數不宣告會預設為string型別

5.檔案表示式

if [ -f  file ]    如果檔案存在if [ -d ...   ]    如果目錄存在if [ -s file  ]    如果檔案存在且非空if [ -r file  ]    如果檔案存在且可讀if [ -w file  ]    如果檔案存在且可寫if [ -x file  ]    如果檔案存在且可執行

整數變量表達式

if [ int1 -eq int2 ]    如果int1等於int2   if [ int1 -ne int2 ]    如果不等於 if [ int1 -ge int2 ]       如果

>=if [ int1 -gt int2 ]       如果>if [ int1 -le int2 ]       如果<=if [ int1 -lt int2 ]       如果<

字串變量表達式

If  [ $a = $b ]                 如果string1等於string2                                字串允許使用賦值號做等號if  [ $string1 !=  $string2 ]   如果string1不等於string2       if  [ -n $string  ]             如果

string 非空(0),返回0(true)  if  [ -z $string  ]             如果string 為空if  [ $sting ]                  如果string 非空,返回0 (-n類似)