書-Linux命令行與shell腳本編程大全(第3版)

★shell-腳本

★shell-顯示

★shell-變數

★重定向輸出

★expr-指令

★方括號

 

shell-腳本

$vim ba.sh

image

第一行是你要用什麼shell執行,'#'字號是註解,但是第一行例外,會被解讀成用bash執行,最後結束時用'exit 0'。

$sh ba.sh  //執行

image

image

$sudo chmod 777 ba.sh //如果有權限問題就用這個

如果你有加環境變數的話就可以只打'ba'就可以執行,所以把上面的名稱改為'ba'。

$mv ba.sh ba  //改名稱

我現在在'/home/free',查看PATH是否有。

$echo ${PATH}

image

可以發現沒有所以我們要添加。

$PATH=$PATH:/home/free

image

image

結果:

image

shell-顯示

'echo'就是用來顯示

$echo hello world

image

$echo -n //就是不換行

image

image

shell-變數

$set //查看變數

image

image

使用'$'號去顯示,也可以${EUID}。

image

自己定義的變數。

image

image

重定向輸出

就是你要把輸出結果寫在文件裡,就可以用這個方法。

$date>t7   //把date的輸出寫入t7這個文件中

image

image

$who>t7 //在寫入會被覆蓋

image

image

$date>>t7 //這樣就不會被覆蓋

image

image

expr-指令

簡單的算術與邏輯判斷

$expr 5 + 10

image

$expr 3 * 5  // *號在shell中有特殊的含意,我以會報錯

image

$expr 3 \* 5  //加上反斜線就可以辨別

image

image

如果要在shell腳本中進行數學的運算,就要這樣寫,比較麻煩,下面會用其他的方式。

$va3=$(expr $va1 + $va2)

image

image

方括號

在方括號中就是在做數學的運算,可以不用加指令expr,'*'號也不用加反斜線。

 

image

image

arrow
arrow
    創作者介紹
    創作者 讀書小天地 的頭像
    讀書小天地

    書籍分享天地

    讀書小天地 發表在 痞客邦 留言(0) 人氣()