(1)數學運算
例程1:
#/bin/bash
echo "input your first number"
read num1
echo "input your second number"
read num2
total1=$(($num1*$num2)) #求積
total2=$(($num1%$num2)) #求餘
echo "total=$total"
注意:數學運算的這種寫法: var=$(()) #兩個括號,或declare -i var,這樣聲明瞭var是一整型數.
例程1:
#/bin/bash
echo "input your first number"
read num1
echo "input your second number"
read num2
total1=$(($num1*$num2)) #求積
total2=$(($num1%$num2)) #求餘
echo "total=$total"
注意:數學運算的這種寫法: var=$(()) #兩個括號,或declare -i var,這樣聲明瞭var是一整型數.
(2)判斷條件test用法
例程2:
1.用戶首先輸入一文件名,判斷該文件是否存在.若不存在,則中斷程序.
2.若文件存在,判斷它是目錄還是文件.
3.判斷執行者對該文件或目錄所擁有的權限.
#!/bin/bash
echo "Please input filename"
read filename
test -e $filename && echo "$filename exit" || echo "$filename not exit" || exit 0
test -d $filename && echo "$filename is directory"
test -f $filename && echo "$filename is a regular file"
test -r $filename && echo "$filename r"
test -w $filename && echo "$filename w"
test -x $filename && echo "$filenmae x"
執行結果:
Please input filename
/home/lishuai exit
/home/lishuai is directory
/home/lishuai r
/home/lishuai w
/home/lishuai x
(3)判斷符號[]用法
例程3:
1.當執行一程序時,會讓用戶選擇Y或N.
2.若用戶輸入Y或y時,提示"ok,continue".
3.若用戶輸入N或n時,提示"Oh,interrupt!".
4.若用戶不是輸入Y/y、N/n,提示"i do not know your choice!".
#!/bin/bash
echo "Please input your choice:"
read choice
[ "&choice == "Y" -o "$choice == "y" ] && echo "Ok,continue" && exit 0
[ "&choice == "N" -o "$choice == "n" ] && echo "Oh,interrupt" && exit 0
echo "I do not know your choice" && exit 0
例程4:
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder" #/home目錄文件可讀,故執行echo後的語句
[ -f "$folder" ] || echo "this is not file" #/home是目錄文件,不是普通文件,故執行echo後的語句
[ -e "$folder" ] && echo "$folder exit" #/home目錄存在,故執行echo後的語句
[ -s "$folder" ] || echo "$folder is 0" #/home目錄大小不爲0,故不執行echo後的語句
執行結果:
can read /home
this is not file
/home exit
從該例程可以看出,判斷條件(test)與判斷符號([])的用法十分類似.
例程2:
1.用戶首先輸入一文件名,判斷該文件是否存在.若不存在,則中斷程序.
2.若文件存在,判斷它是目錄還是文件.
3.判斷執行者對該文件或目錄所擁有的權限.
#!/bin/bash
echo "Please input filename"
read filename
test -e $filename && echo "$filename exit" || echo "$filename not exit" || exit 0
test -d $filename && echo "$filename is directory"
test -f $filename && echo "$filename is a regular file"
test -r $filename && echo "$filename r"
test -w $filename && echo "$filename w"
test -x $filename && echo "$filenmae x"
執行結果:
Please input filename
/home/lishuai exit
/home/lishuai is directory
/home/lishuai r
/home/lishuai w
/home/lishuai x
(3)判斷符號[]用法
例程3:
1.當執行一程序時,會讓用戶選擇Y或N.
2.若用戶輸入Y或y時,提示"ok,continue".
3.若用戶輸入N或n時,提示"Oh,interrupt!".
4.若用戶不是輸入Y/y、N/n,提示"i do not know your choice!".
#!/bin/bash
echo "Please input your choice:"
read choice
[ "&choice == "Y" -o "$choice == "y" ] && echo "Ok,continue" && exit 0
[ "&choice == "N" -o "$choice == "n" ] && echo "Oh,interrupt" && exit 0
echo "I do not know your choice" && exit 0
例程4:
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder" #/home目錄文件可讀,故執行echo後的語句
[ -f "$folder" ] || echo "this is not file" #/home是目錄文件,不是普通文件,故執行echo後的語句
[ -e "$folder" ] && echo "$folder exit" #/home目錄存在,故執行echo後的語句
[ -s "$folder" ] || echo "$folder is 0" #/home目錄大小不爲0,故不執行echo後的語句
執行結果:
can read /home
this is not file
/home exit
從該例程可以看出,判斷條件(test)與判斷符號([])的用法十分類似.
(4)條件判斷if...then...的用法
例程5(該例程是例程3的拓展):if...then...
#!/bin/bash
echo "Please input your choice"
read choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "ok,continue"
exit 0
fi
if [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh,interrupt"
exit 0
fi
echo "I do not know your choice,Please input again" && exit 0
Attention!!!
<1>中括號中各個組件之間必須以空格作爲分隔符.
<2>中括號外各個組件之間必須以空格作爲分隔符.如中括號與if之間有一空格,中括號與"||"之間有一空格.
<3>";"與方括號"]"之間有沒有空格都無所謂,表示這一條語句的結束.推薦加上空格.
<4>"then"與";"之間有沒有空格都無所謂.推薦加上空格.
<5>在中括號內各組件均用雙引號引起來.
例程6(該例程是例程3的拓展):if...elif...else
#!/bin/bash
echo "Please input your choice"
read choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "ok,continue"
exit 0
elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh,interrupt"
exit 0
else
echo "I do not know your choice,Please input again" && exit 0
fi
例程7:
1.在程序後接第一個參數是否爲"hello",如果是,則執行相應動作.
2.如果沒有加任何參數,則執行相應動作.
3.如果加入的參數不是hello,則執行相應的動作.
#!/bin/bash
echo "please excute your result with parameter"
if [ "$1" = "hello" ];then
echo "hello embeded"
elif [ -z "$1" ];then
echo "you must input parameter"
elif [ "$1" != "hello" ];then
echo "you must input the first parameter:hello"
else
echo "you are wrong!"
fi
例程5(該例程是例程3的拓展):if...then...
#!/bin/bash
echo "Please input your choice"
read choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "ok,continue"
exit 0
fi
if [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh,interrupt"
exit 0
fi
echo "I do not know your choice,Please input again" && exit 0
Attention!!!
<1>中括號中各個組件之間必須以空格作爲分隔符.
<2>中括號外各個組件之間必須以空格作爲分隔符.如中括號與if之間有一空格,中括號與"||"之間有一空格.
<3>";"與方括號"]"之間有沒有空格都無所謂,表示這一條語句的結束.推薦加上空格.
<4>"then"與";"之間有沒有空格都無所謂.推薦加上空格.
<5>在中括號內各組件均用雙引號引起來.
例程6(該例程是例程3的拓展):if...elif...else
#!/bin/bash
echo "Please input your choice"
read choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
echo "ok,continue"
exit 0
elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
echo "Oh,interrupt"
exit 0
else
echo "I do not know your choice,Please input again" && exit 0
fi
例程7:
1.在程序後接第一個參數是否爲"hello",如果是,則執行相應動作.
2.如果沒有加任何參數,則執行相應動作.
3.如果加入的參數不是hello,則執行相應的動作.
#!/bin/bash
echo "please excute your result with parameter"
if [ "$1" = "hello" ];then
echo "hello embeded"
elif [ -z "$1" ];then
echo "you must input parameter"
elif [ "$1" != "hello" ];then
echo "you must input the first parameter:hello"
else
echo "you are wrong!"
fi
(5)條件判斷case...esac的用法
case...esac語句常用在字符串匹配.比如Linux的服務啓動放置目錄在/etc/init.d內,其中有一syslog服務,若要重啓該服務,必須執行:
/etc/init.d/syslog restart
重點是restart.腳本/etc/init.d/syslog可以執行不同的動作,具體什麼動作,由該腳本的第一個參數來決定,此時就可以使用case...esac來判斷.
例程8(該例程是例程7的拓展):
#!/bin/bash
case $1 in
"hello")
echo "hello embeded"
;;
"")
echo "you must input parameter with hello"
;;
*)
echo "use $0 {hello}"
;;
esac
從該例程可以類推如何使/etc/init.d/syslog執行不同的動作.這點經常遇到.比如重啓nfs服務器,則輸入命令service nfs restart;查看Samba服務器的狀態,則輸入命令service smb status,(自己理解)這些命令的輸出信息就可以通過case...esac語句而實現.比如當可執行命令的第一個參數是restart,則表示重啓該服務器;當可執行命令的第一個參數是status,則表示要查詢服務器的狀態,可用echo將服務器狀態輸出.
Attenton!!!
case...esac條件判斷語句是通過不同的變量值來執行不同的動作.注意,該變量值可由兩種途徑獲取.一種是執行某可執行程序時直接由參數獲取,另一種是通過read從終端鍵盤獲取.
例程9
#!/bin/bash
echo "Hit a key,then hit return."
read Keypress
case "Keypress" in
[A-Z])
echo "Uppercase letter";;
[a-z])
echo "Lowercase letter";;
[0-9])
echo "Digit";;
*)
echo "Punctuation,whitespace,or other";;
esac
(6)Shell函數的用法
例程10:該例程可用Shell函數來實現,也可用case...esac來實現.
#!/bin/bash
#在腳本的開頭定義一函數
function hehe(){
echo -n "your choice is" # -n表示下一個輸出不在新一行,仍在當前行
}
echo "Please input your choice:"
case $1 in
"one")
hehe;echo $1
;;
"two")
hehe;echo $1
;;
"three")
hehe;echo $1
;;
*)
echo "you are wrong"
;;
esac
執行結果:
(1)若執行#sh test9.sh one ,則輸出:your choice is one
(2)若執行#sh test9.sh two ,則輸出:your choice is two
(3)若執行#sh test9.sh three ,則輸出:your choice is three
(4)若執行#sh test9.sh four ,則輸出:your choice is you are wrong
(自己的理解)可以這樣來理解,由於執行結果中都有"your choice is",爲了簡化程序代碼,可以將這條語句封裝成函數.實際執行時,比如執行hehe;echo $1 時,可以理解爲 echo -n "your choice is" echo $1
例程11:練習函數的參數
Shell腳本中的函數也有自己的變量,其中,$0表示函數名,$1表示該函數的第一個參數,$2表示該函數的第二個參數,以此類推.注意,這裏的$0、$1、$2...與腳本的$0、$1、$2...所表示的含義不同.前者是函數的參數,後者是腳本內函數的參數.
test10.sh
#!/bin/bash
function haha(){
echo "your choice is $1" #由於(第一次出現的)$1在函數體內部,因此$1表示函數haha()的第一個參數
}
echo "Please input your choice:"
case $1 in #由於(第二次出現的)$1在函數體外部,因此$1表示腳本文件test10.sh的第一個參數
"one")
haha 1 #1直接加在函數haha()的後面,表示該函數的第一個參數就是1,即第一次出現的$1的值爲1
;;
"two")
haha 2 #2直接加在函數haha()的後面,表示該函數的第一個參數就是2,即第一次出現的$1的值爲2
;;
"three")
haha 3 #3直接加在函數haha()的後面,表示該函數的第一個參數就是3,即第一次出現的$1的值爲3
;;
*)
echo "you are wrong"
;;
esac
執行結果:
[root@localhost lishuai]# sh test10.sh one
輸出結果: your choice is 1
[root@localhost lishuai]# sh test10.sh two
輸出結果: your choice is 2
[root@localhost lishuai]# sh test10.sh three
輸出結果: your choice is 3
case...esac語句常用在字符串匹配.比如Linux的服務啓動放置目錄在/etc/init.d內,其中有一syslog服務,若要重啓該服務,必須執行:
/etc/init.d/syslog restart
重點是restart.腳本/etc/init.d/syslog可以執行不同的動作,具體什麼動作,由該腳本的第一個參數來決定,此時就可以使用case...esac來判斷.
例程8(該例程是例程7的拓展):
#!/bin/bash
case $1 in
"hello")
echo "hello embeded"
;;
"")
echo "you must input parameter with hello"
;;
*)
echo "use $0 {hello}"
;;
esac
從該例程可以類推如何使/etc/init.d/syslog執行不同的動作.這點經常遇到.比如重啓nfs服務器,則輸入命令service nfs restart;查看Samba服務器的狀態,則輸入命令service smb status,(自己理解)這些命令的輸出信息就可以通過case...esac語句而實現.比如當可執行命令的第一個參數是restart,則表示重啓該服務器;當可執行命令的第一個參數是status,則表示要查詢服務器的狀態,可用echo將服務器狀態輸出.
Attenton!!!
case...esac條件判斷語句是通過不同的變量值來執行不同的動作.注意,該變量值可由兩種途徑獲取.一種是執行某可執行程序時直接由參數獲取,另一種是通過read從終端鍵盤獲取.
例程9
#!/bin/bash
echo "Hit a key,then hit return."
read Keypress
case "Keypress" in
[A-Z])
echo "Uppercase letter";;
[a-z])
echo "Lowercase letter";;
[0-9])
echo "Digit";;
*)
echo "Punctuation,whitespace,or other";;
esac
(6)Shell函數的用法
例程10:該例程可用Shell函數來實現,也可用case...esac來實現.
#!/bin/bash
#在腳本的開頭定義一函數
function hehe(){
echo -n "your choice is" # -n表示下一個輸出不在新一行,仍在當前行
}
echo "Please input your choice:"
case $1 in
"one")
hehe;echo $1
;;
"two")
hehe;echo $1
;;
"three")
hehe;echo $1
;;
*)
echo "you are wrong"
;;
esac
執行結果:
(1)若執行#sh test9.sh one ,則輸出:your choice is one
(2)若執行#sh test9.sh two ,則輸出:your choice is two
(3)若執行#sh test9.sh three ,則輸出:your choice is three
(4)若執行#sh test9.sh four ,則輸出:your choice is you are wrong
(自己的理解)可以這樣來理解,由於執行結果中都有"your choice is",爲了簡化程序代碼,可以將這條語句封裝成函數.實際執行時,比如執行hehe;echo $1 時,可以理解爲 echo -n "your choice is" echo $1
例程11:練習函數的參數
Shell腳本中的函數也有自己的變量,其中,$0表示函數名,$1表示該函數的第一個參數,$2表示該函數的第二個參數,以此類推.注意,這裏的$0、$1、$2...與腳本的$0、$1、$2...所表示的含義不同.前者是函數的參數,後者是腳本內函數的參數.
test10.sh
#!/bin/bash
function haha(){
echo "your choice is $1" #由於(第一次出現的)$1在函數體內部,因此$1表示函數haha()的第一個參數
}
echo "Please input your choice:"
case $1 in #由於(第二次出現的)$1在函數體外部,因此$1表示腳本文件test10.sh的第一個參數
"one")
haha 1 #1直接加在函數haha()的後面,表示該函數的第一個參數就是1,即第一次出現的$1的值爲1
;;
"two")
haha 2 #2直接加在函數haha()的後面,表示該函數的第一個參數就是2,即第一次出現的$1的值爲2
;;
"three")
haha 3 #3直接加在函數haha()的後面,表示該函數的第一個參數就是3,即第一次出現的$1的值爲3
;;
*)
echo "you are wrong"
;;
esac
執行結果:
[root@localhost lishuai]# sh test10.sh one
輸出結果: your choice is 1
[root@localhost lishuai]# sh test10.sh two
輸出結果: your choice is 2
[root@localhost lishuai]# sh test10.sh three
輸出結果: your choice is 3
(7)循環(while...do...done、until...do...done)的用法
例程12:假如要讓用戶輸入yes或YES才結束程序,否則一直提示用戶.
#!/bin/bash
echo "Please input your choice:"
read input
while [ "$input" != "yes"] && [ "$input" != "YES" ]
do
echo -p "please input yes/YES to stop your program" input
done
#!/bin/bash
echo "Please input your choice:"
read input
until [ "$input" == "yes" ] || [ "$input" == "YES" ]
do
echo -p "please input yes/YES to stop your program" input
done
例程13:計算1+2+3+...+100
方法1:
#!/bin/bash
i=0
sum=0
while [ "$i" != "100" ]
do
i=$(($i+1))
sum=$(($sum+$i))
done
echo "the result is $sum"
方法2:
#!/bin/bash
declare -i i=0
declare -i sum=0
while [ "$i" != "100" ] #使用declare -i聲明後,表示變量i和sum都是整數,要取其值,仍要加上$
do
i=$i+1
sum=$sum+$i
done
echo "the result is $sum"
(8)循環(for...do...done)的用法
例程14: 計算1+2+3+...+100
#!/bin/bash
sum=0
for (( i=0; i<=100; i=i+1 )) #也可以寫成 for (( i=0; i<=100; i++ ))
do
sum=$(($sum+$i))
done
echo "the result is $sum"
例程15:
#!/bin/sh
for day in Sun Mon Tue Wed Thu Fri Sat
do
echo $day
done
執行結果:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
例程16:如果列表被包含在一對雙引號中,則被認爲是一個元素
#!/bin/sh
for day in "Sun Mon Tue Wed Thu Fri Sat"
do
echo $day
done
執行結果:
Sun Mon Tue Wed Thu Fri Sat
(9)綜合例程
例程17:
例程12:假如要讓用戶輸入yes或YES才結束程序,否則一直提示用戶.
#!/bin/bash
echo "Please input your choice:"
read input
while [ "$input" != "yes"] && [ "$input" != "YES" ]
do
echo -p "please input yes/YES to stop your program" input
done
#!/bin/bash
echo "Please input your choice:"
read input
until [ "$input" == "yes" ] || [ "$input" == "YES" ]
do
echo -p "please input yes/YES to stop your program" input
done
例程13:計算1+2+3+...+100
方法1:
#!/bin/bash
i=0
sum=0
while [ "$i" != "100" ]
do
i=$(($i+1))
sum=$(($sum+$i))
done
echo "the result is $sum"
方法2:
#!/bin/bash
declare -i i=0
declare -i sum=0
while [ "$i" != "100" ] #使用declare -i聲明後,表示變量i和sum都是整數,要取其值,仍要加上$
do
i=$i+1
sum=$sum+$i
done
echo "the result is $sum"
(8)循環(for...do...done)的用法
例程14: 計算1+2+3+...+100
#!/bin/bash
sum=0
for (( i=0; i<=100; i=i+1 )) #也可以寫成 for (( i=0; i<=100; i++ ))
do
sum=$(($sum+$i))
done
echo "the result is $sum"
例程15:
#!/bin/sh
for day in Sun Mon Tue Wed Thu Fri Sat
do
echo $day
done
執行結果:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
例程16:如果列表被包含在一對雙引號中,則被認爲是一個元素
#!/bin/sh
for day in "Sun Mon Tue Wed Thu Fri Sat"
do
echo $day
done
執行結果:
Sun Mon Tue Wed Thu Fri Sat
(9)綜合例程
例程17:
1.用戶輸入任意目錄,找出該目錄內文件名的權限.
2.列出只具有可執行權限的文件
#!/bin/bash
echo "Please input directory"
read dir
if [ "$dir" == "" ] || [ ! -d "$dir" ] #判斷該目錄是否爲空,或並非目錄文件
then
echo "$dir not exit! Please input again!"
exit 0
fi
filelist=`ls $dir` #將 ls $dir 的返回值賦給變量filelist,這裏必須使用優先執行符,否則視爲字符串
echo "the content of $dir is $filelist"
for filename in $filelist #變量filename可取的值都在變量$filelist中
do
test -r "$dir/$filename" && echo "$filename readable"
test -w "$dir/$filename" && echo "$filename writeable"
test -x "$dir/$filename" && echo "$filename executable"
done
for filename in $filelist
do
[ -x "$dir/$filename" ] && echo "$filename excutable" #只列出具有可執行權限的文件
done
2.列出只具有可執行權限的文件
#!/bin/bash
echo "Please input directory"
read dir
if [ "$dir" == "" ] || [ ! -d "$dir" ] #判斷該目錄是否爲空,或並非目錄文件
then
echo "$dir not exit! Please input again!"
exit 0
fi
filelist=`ls $dir` #將 ls $dir 的返回值賦給變量filelist,這裏必須使用優先執行符,否則視爲字符串
echo "the content of $dir is $filelist"
for filename in $filelist #變量filename可取的值都在變量$filelist中
do
test -r "$dir/$filename" && echo "$filename readable"
test -w "$dir/$filename" && echo "$filename writeable"
test -x "$dir/$filename" && echo "$filename executable"
done
for filename in $filelist
do
[ -x "$dir/$filename" ] && echo "$filename excutable" #只列出具有可執行權限的文件
done