linux shell 學習(下)

- eq —比較兩個參數是否相等(例如,if [ 2 –eq 5 ])
-ne —比較兩個參數是否不相等
-lt —參數1是否小於參數2
-le —參數1是否小於等於參數2
-gt —參數1是否大於參數2
-ge —參數1是否大於等於參數2
-f — 檢查某文件是否存在(例如,if [ -f "filename" ])
-d — 檢查目錄是否存在

幾乎所有的判斷都可以用這些比較運算符實現。腳本中常用-f命令選項在執行某一文件之前檢查它是否存在


if [空格 表達式 空格]

 then 執行語句

elif[空格 表達式 空格]

then 執行語句

else 執行語句

fi

比如

a=1

b=2

if [ $a == $b ]

then echo "a==b"

elif [ $a -gt $b ]

then echo "a>b"

else echo "a<b"

fi

if ... else 語句也可以寫成一行,以命令的方式來運行,像這樣:

if [ $a == $b ];then echo "a==b";else echo "a!=b";

或者

if test $a -eq $b

then echo "a==b"

else echo "a!=b"

fi

test 命令用於檢查某個條件是否成立,與方括號([ ])類似

二、

case 值 in
模式1)
    command1
    command2
    command3
    ;;
模式2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac
比如


echo "input your number"
read num
case $num in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3) echo 'you select 3'
;;
4) echo 'you select 4'
;;
*) echo 'you select num not bt 1-4'
;;
esac

三、

for循環一般格式爲:

for 變量 in 列表
do
    command1
    command2
    ...
    commandN
done
列表是一組值(數字、字符串等)組成的序列,每個值通過空格分隔。每循環一次,就將列表中的下一個值賦給變量。

in 列表是可選的,如果不用它,for 循環使用命令行的位置參數。

比如

for x in this is phf
do
  echo $x
done

輸出

this

is

phf

比如

for str in "this is phf"
do
  echo $x
done

輸出 this is phf  //當做一個字符串了 有雙引號或者單引號

四、while

while command
do
   Statement(s) to be executed if command is true
done
比如:

num=0
while [ $num -lt 5 ]
do
 num=`expr $num + 1`
echo $num
done

比如 從鍵盤讀取信息

 echo "enter your most like movie:"
while read movie
do echo "the name is $movie"
done

五、 until

until 循環執行一系列命令直至條件爲 true 時停止。until 循環與 while 循環在處理方式上剛好相反。一般while循環優於until循環,但在某些時候,也只是極少數情況下,until 循環更加有用

until 循環格式爲:

until command
do
   Statement(s) to be executed until command is true
done
command 一般爲條件表達式,如果返回值爲 false,則繼續執行循環體內的語句,否則跳出循環

num=5
until [ $num -eq 2 ]
do num=`expr $num - 1`
echo "num=$num"
done

六、函數

# Define your function hereHello () { echo "Url is http://see.xidian.edu.cn/cpp/shell/"}
# Invoke your functionHello
輸出
Url is http://see.xidian.edu.cn/cpp/shell/
注意 調用函數時只需要給出函數名即可

來看一個帶返回值的

#!/bin/bash
funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
# 使用$?捕獲函數返回值
ret=$?
echo "The sum of two numbers is $ret !"
像刪除變量一樣,刪除函數也可以使用 unset 命令,不過要加上 .f 選項,如下所示:

$unset .f function_name
七、函數參數

在Shell中,調用函數時可以向其傳遞參數。在函數體內部,通過 $n 的形式來獲取參數的值,例如,$1表示第一個參數,$2表示第二個參數..

八、重定向

九、/dev/null            http://blog.csdn.net/pi9nc/article/details/18257593

 (1)、/dev/null  : 在類Unix系統中,/dev/null,或稱空設備是一個特殊的設備文件,它丟棄一切寫入其中的數據(但報告寫入操作成功),讀取它則會立即得到一個EOF。
在程序員行話,尤其是Unix行話中,/dev/null 被稱爲位桶(bit bucket)或者黑洞(black hole)。空設備通常被用於丟棄不需要的輸出流,或作爲用於輸入流的空文件

2、 /dev/null 的日常使用

把/dev/null看作"黑洞"。它等價於一個只寫文件,並且所有寫入它的內容都會永遠丟失,而嘗試從它那兒讀取內容則什麼也讀不到。然而, /dev/null對命令行和腳本都非常的有用

 補充:我們知道cat filename會輸出文件內容到標準輸出,如果filename不存在會輸出錯誤信息到標準錯誤輸出,如果我們不想看到錯誤輸出則可以

cat filename 2>/dev/null

如果想將stdout 和stderr都重定向 則 cat filename>/dev/null 2>&1

我們使用 echo $? 查看上條命令的退出碼:0爲命令正常執行,1-255爲有出錯。

3、/dev/zero

dev/zero  : 在類UNIX 操作系統中, /dev/zero 是一個特殊的文件,當你讀它的時候,它會提供無限的空字符(NULL, ASCII NUL, 0x00)




備註:關於shell 的 看這篇文章  都寫的很詳細


http://c.biancheng.net/cpp/view/6994.html

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