shell命令總結3

1.[root@localhost ~]# date "+%Y%m%d %H%M%S" //在腳本中date這個命令它的作用是用來打印當前系統時間。%Y表示年,%m表示月,%d表示日期,%H表示小時,%M表示分鐘,%S表示秒

20130402 104156

2.[root@localhost sbin]# cat zh888.sh // 編寫一個求和的腳本
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02

a=1
b=2
sum=$[$a+$b]
echo "sum is $sum"

數學計算要用’[ ]’括起來並且外頭要帶一個’$’。腳本結果爲:
[root@localhost sbin]# sh zh888.sh
sum is 3


3.[root@localhost sbin]# cat zh888.sh  //shell還可以交互模式
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02

echo "please input a number:"
read x
echo "please input another number:"
read y
sum=$[$x+$y]
echo "the sum of tow numbers is: $sum"

[root@localhost sbin]# sh zh888.sh //這就用到了read命令了,它可以從標準輸入獲得變量的值,後跟變量名。”read x”表示x變量的值需要用戶通過鍵盤輸入得到。腳本執行過程如下:
平時在sh -x zh888.sh來查看執行過程用於排錯。

please input a number:
22
please input another number:
33
the sum of two numbers is: 55


4.[root@localhost sbin]# cat zh888.sh //read -p 選項類似echo的作用
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02

read -p "please input a number:" x
read -p "please input another number:" y
sum=$[$x+$y]
echo "the sum of tow numbers is: $sum"

[root@localhost sbin]# sh zh888.sh //執行腳本如下:
please input a number:22s
please input another number:33
the sum of two numbers is: 55

5.[root@localhost sbin]# cat zh888.sh //在腳本中,你會不會奇怪,哪裏來的$1和$2,這其實就是shell腳本的預設變量,其中$1的值就是在執行的時候輸入的1,而$2的值就是執行的時候輸入的$2,當然一個shell腳本的預設變量是沒有限制的

#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
sum=$[$1+$2]
echo $sum


[root@localhost sbin]# sh -x zh888.sh 1 33//執行過程如下:
+ sum=34
+ echo 34
34

6.[root@localhost sbin]# cat zh888.sh //另外還有一個$0,不過它代表的是腳本本身的名字。不妨把腳本修改一下。
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
echo "$0 $1 $2"

[root@localhost sbin]# sh zh888.sh 1 3 //執行結果如下:
zh888.sh 1 3


7.[root@localhost sbin]# cat ifzh888.sh ///出現了 ((a<60))這樣的形式,這是shell腳本中特有的格式,用一個小括號或者不用都會報錯,請記住這個格式.
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02

read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
fi

在shell腳本中我們同樣可以使用if邏輯判斷。在shell中if判斷的基本語法爲:

1)不帶else

if 判斷語句; then

command

fi


2)帶有else

if 判斷語句 ; then

command

else

command

fi

[root@localhost sbin]#cat ifzh888.sh //添加else語句:
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02

read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
else
echo "good you passed the exam."
fi

[root@localhost sbin]# sh -x ifzh888.sh //執行過程如下:
+ read '-pplease input you score:' a
please input you score:23
+ (( a<60 ))
+ echo 'you didn'\''t pass the exam.'
you didn't pass the exam.
[root@localhost sbin]# sh -x ifzh888.sh
+ read '-pplease input you score:' a
please input you score:78
+ (( a<60 ))
+ echo 'good you passed the exam'
good you passed the exam


3)帶有elif

if 判斷語句一 ; then

command

elif 判斷語句二; then

command

else

command

fi

[root@localhost sbin]# cat ifzh888.sh //腳本修改如下:
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02
read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
elif ((a>60))&&((a<85));then
echo "good! you passed the exam"
else
echo "very good you score is very high!"
fi

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