各種引號的用法總結如下
1、 單引號 ‘
由單引號括起來的字符都作為普通字符出現。特殊字符用單引號括起來以後,也會失去原有意義,而只作為普通字符解釋。
2、 雙引號 “
由雙引號括起來的字符,除
3、 反引號 `
反引號(`)這個字符所對應的鍵一般位於鍵盤的左上角,不要將其同單引號(’)混淆。反引號括起來的字符串被shell解釋為命令行,在執行時,shell首先執行該命令行,並以它的標准輸出結果取代整個反引號(包括兩個反引號)部分。
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
if [ "$1" = "hello" ]; then
echo "hello, who are you?"
elif [ "$1" = "" ]; then
echo "you must input parameters,ex>{$0 somewprd}"
else
echo "the only parameter is 'hello',ex>{$0 hello}"
fi
~
可以進行空字符串的比較 == "",這裡的{} 只是一個字符
netstat -tuln
127.0.0.1 表示僅對本機開放
0.0.0.0 或者:::: 表示對整個internet開放
不能運行,嘗試的例子
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
netstat_list=$(netstat -tuln)
if [ $($netstat%":80") ]; then
echo ":80"
fi
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
testing=$(netstat -tuln | grep ":80") #$() 命令行之前沒有空格
if [ "$testing" != "" ]; then
echo "www is running in your system"
fi
testing=$( netstat -tuln | grep ":22" ) #$() 命令行之前可以有空格
if [ "$testing" != "" ]; then
echo " ssh is running in your system"
fi
~
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
read -p "please input your time:" lasttime
nowtime=$( date +%s )
lasttime=$( date --date "$lasttime" +%s )
echo $[[$lasttime-$nowtime ]]
exit 0
為什麼輸出來是個這了,沒有進行減法操作(當作了字符串???還是(()) )
fuhui@ubuntu:~/script$ sh sh13.sh
please input your time:20150902
$[[1441177200-1434859352 ]]
正則匹配的測試
fuhui@ubuntu:~/script$ echo 1234 | grep "[0-9]\{3\}"
1234
在{}需要進行轉碼,沒有//這樣的標志
1 declare -i number
2 # 腳本余下的部分會把”number”當作整數看待.
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
read -p "please input your time: ex(20100101)" lasttime
date_d=$( echo $lasttime | grep '[0-9]\{8\}') #重點學習一下正則的匹配
if [ "$date_d" = "" ]; then
echo "you input wrong date format.."
exit 1
fi
declare -i date_dem=`date --date="$date_d" +%s`
declare -i date_new=$( date +%s )
declare -i date_total=$(($date_dem-$date_new))
declare -i date_day=$(($date_total/60/60/24))
if [ $date_day -lt 0 ]; then
echo "you had been demobilization before $((-1*$date_day))"
fi
~
出現了一個錯誤,如下所示:
fuhui@ubuntu:~/script$ sh sh13.sh
please input your time: ex(20100101)20110505
sh13.sh: 13: sh13.sh: declare: not found
sh13.sh: 14: sh13.sh: declare: not found
修改了文件屬性如下,錯誤就沒有了
chmod +x sh13.sh
case語句
case $變量名稱 in <==關鍵詞為 case ,相當於switch的變量
“第一個變量內容”) <==每個變量內容建議用雙引號括起來,關鍵詞則為小括號 )
程序段
;; <==操作結尾使用兩個連續的分號來處理!
“第二個發量內容”)
程序段
;;
) <==最後一個變量內容都會用 來代表所有其他值
類似於default語句,結尾有;;符
;;
esac <==最終的 case 結尾,case的反向書寫形式
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
case $1 in
"hello")
echo "continue to do "
;;
"")
echo "you should input a option"
;;
*)
echo "you must input parameter,ex >$0,someoption"
;;
esac
fuhui@ubuntu:~/script$ sh sh15.sh 3
you must input parameter,ex >sh15.sh,someoption
fuhui@ubuntu:~/script$ sh sh15.sh hello #輸入"hello"和hello居然是一樣的
continue to do
fuhui@ubuntu:~/script$ sh sh15.sh "hello"
continue to do
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
read -p "input one of ont, two, three:" valu
case $valu in
"one")
echo "first"
;;
"two")
echo "second"
;;
"three")
echo "third"
;;
*)
echo "error"
;;
esac
函數聲明必須在shell的最前面
function funName{
}
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
function prifxit(){
echo -n "you choice is: "
}
read -p "input one of ont, two, three:" valu
case $valu in
"one")
prifxit; echo "first"
;;
"two")
prifxit; echo "second"
;;
"three")
prifxit; echo "third"
;;
*)
prifxit; echo "error" | tr 'a-z' 'A-Z' #特別注意大小寫的轉換
;;
esac
特別注意:sh sh17.sh不能運行,提示錯誤。這時候修改文件為可執行,就沒有錯誤了。(雖然不知道為什麼)
cp操作會復制文件的權限
fuhui@ubuntu:~/script$ chmod +x sh17.sh
fuhui@ubuntu:~/script$ ./sh17.sh
input one of ont, two, three:one
you choice is: first
fuhui@ubuntu:~/script$ ./sh17.sh
input one of ont, two, three:ss
you choice is: ERROR
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
function prifxit(){
echo -n "the function name $0 : $1"
}
read -p "input one of ont, two, three:" valu
case $valu in
"one")
prifxit "fuhui"; echo "first"
;;
"two")
prifxit "hui"; echo "second"
;;
"three")
prifxit "do"; echo "third"
;;
*)
prifxit "it"; echo "error" | tr 'a-z' 'A-Z'
;;
esac
函數的例子,函數內的$0並沒有指明這個函數的名字,而是執行腳本的名稱。
注意:函數的$1代表函數的第一個參數,依次類推
fuhui@ubuntu:~/script$ ./sh18.sh
input one of ont, two, three:
the function name ./sh18.sh : itERROR
fuhui@ubuntu:~/script$ ./sh18.sh
input one of ont, two, three:one
the function name ./sh18.sh : fuhuifirst
while [ condition ] <==中括號內為判斷式
do <==do 循環的開始!
程序段落
done <==done 循環的結束
until [ condition ]
do
程序段落
done
兩個循環中值得條件不一樣,正好相反。
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
while [ "$yn" != "yes" -a "$yn" != "YES" ] #特別注意-a是&&的意思,-o是||的意思 ,支持 !=
do
read -p "please input a option :" yn
done
echo "you option is right"
until的用法,注意准換大小寫的用法
V=hello
V=`echo $V | tr '[:lower:]' '[:upper:]'`
echo $V
下面的轉化效果是一樣的
echo as | tr '[:lower:]' '[:upper:]'
echo as | tr [:lower:] [:upper:]
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
until [ "$yn" = "YES" ]
do
read -p "please input a option :" yn
yn=$(echo $yn | tr 'a-z' 'A-Z')
if [ "$yn" = "EXIT" ]; then
echo "exit program"
break;
fi
done
計算1+2+。。。+100
declare -i num=0
done
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
declare -i sum=0
declare -i num=0
while [ $num -le 100 ] #特別注意 -le 平時使用的時候都是 test $num -le 100
do
sum=$(($sum+$num)) #需要注意$sum 和sum的區別
num=$(($num+1))
done
echo "the sum is: $sum"
exit 0
~
echo "you option is right"