shell script 是利用 shell 的功能所寫(xiě)的一個(gè)程序,這個(gè)程序使用純文本文件,將一些 shell 的語(yǔ)法和命令寫(xiě)在里面,搭配正則表達(dá)式,管道命令與數(shù)據(jù)流重定向等功能,達(dá)到我們想要的目的
直接命令執(zhí)行
shell script 文件必須具備 rx 的權(quán)限,假設(shè) my.sh 在 /root 下
絕對(duì)路徑
[root@bogon ~]# /root/my.sh
相對(duì)路徑
[root@bogon ~]# . /my.sh
bash(sh)命令執(zhí)行
shell script 文件必須具備 r 的權(quán)限
root@bogon ~]# sh my.sh
source 或.命令執(zhí)行
source 或. 命令執(zhí)行與上面執(zhí)行不同,上面執(zhí)行 shell 都會(huì)分配一個(gè)子進(jìn)程來(lái)進(jìn)行,source 或. 命令就在本進(jìn)程執(zhí)行,也就是說(shuō)一些非環(huán)境變量也能讀取到
root@bogon ~]# source my.sh
前面提到 shell script 中可以使用 shell 命令,那么 shell script 格式是怎樣的?
shellscript 文件 my.sh
#!/bin/bash
#This is my first shell script
echo "hello world"
第1行:必須要以#!/bin/bash 開(kāi)頭代表這個(gè)文件內(nèi)的語(yǔ)法使用 bash 的語(yǔ)法
第2行:注釋以#開(kāi)頭
第3行:shell 命令
test測(cè)試命令
用來(lái)檢測(cè)系統(tǒng)上面某些文件或者相關(guān)的屬性
測(cè)試的標(biāo)志 | 代表意義 |
---|---|
1. 關(guān)于某個(gè)文件名的[文件類(lèi)型]判斷,如 test -e filename 表示存在否 | |
-e | 該[文件名]是否存在?(常用) |
-f | 該[文件名]是否存在且為文件(file)?(常用) |
-d | 該[文件名]是否存在且為目錄(directory)?(常用) |
-b | 該[文件名]是否存在且為一個(gè) block device 裝置? |
-c | 該[文件名]是否存在且為一個(gè) character device 裝置? |
-S | 該[文件名]是否存在且為一個(gè) Socket 文件? |
-p | 該[文件名]是否存在且為一個(gè) FIFO (pipe) 文件? |
-L | 該[文件名]是否存在且為一個(gè)連結(jié)檔? |
2. 關(guān)于文件的權(quán)限偵測(cè),如 test -r filename 表示可讀否 (但 root 權(quán)限常有例外) | |
---|---|
-r | 偵測(cè)該文件名是否存在且具有[可讀]的權(quán)限? |
-w | 偵測(cè)該文件名是否存在且具有[可寫(xiě)]的權(quán)限? |
-x | 偵測(cè)該文件名是否存在且具有[可運(yùn)行]的權(quán)限? |
-u | 偵測(cè)該文件名是否存在且具有[SUID]的屬性? |
-g | 偵測(cè)該文件名是否存在且具有[SGID]的屬性? |
-k | 偵測(cè)該文件名是否存在且具有[Sticky bit]的屬性? |
-s | 偵測(cè)該文件名是否存在且為[非空白文件]? |
3. 兩個(gè)文件之間的比較,如: test file1 -nt file2 | |
---|---|
-nt | (newer than)判斷 file1 是否比 file2 新 |
-ot | (older than)判斷 file1 是否比 file2 舊 |
-ef | 判斷 file1 與 file2 是否為同一文件,可用在判斷 hard link 的判定上。 主要意義在判定,兩個(gè)文件是否均指向同一個(gè) inode 哩! |
4. 關(guān)于兩個(gè)整數(shù)之間的判定,例如 test n1 -eq n2 | |
---|---|
-eq | 兩數(shù)值相等 (equal) |
-ne | 兩數(shù)值不等 (not equal) |
-gt | n1 大于 n2 (greater than) |
-lt | n1 小于 n2 (less than) |
-ge | n1 大于等于 n2 (greater than or equal) |
-le | n1 小于等于 n2 (less than or equal) |
5. 判定字串的數(shù)據(jù) | |
---|---|
test -z string | 判定字串是否為 0 ?若 string 為空字串,則為 true |
test -n string | 判定字串是否非為 0 ?若 string 為空字串,則為 false。注: -n 亦可省略 |
test str1 = str2 | 判定 str1 是否等于 str2 ,若相等,則回傳 true |
test str1 != str2 | 判定 str1 是否不等于 str2 ,若相等,則回傳 false |
6. 多重條件判定,例如: test -r filename -a -x filename | |
---|---|
-a | (and)兩種情況同時(shí)成立!例如 test -r file -a -x file,則 file 同時(shí)具有 r 與 x 權(quán)限時(shí),才回傳 true。 |
-o | (or)兩種情況任何一個(gè)成立!例如 test -r file -o -x file,則 file 具有 r 或 x 權(quán)限時(shí),就可回傳 true。 |
! | 反相狀態(tài),如 test ! -x file ,當(dāng) file 不具有 x 時(shí),回傳 true |
舉例
shellscript 文件內(nèi)容:
#!bin/bash
read -p "please input file/dir name: " name
echo "your input is $name"
test ! -e $name && echo "file is not exist! "&&exit 1
test -d $name &&echo "file is directory"
test -f $name &&echo "file is regulate file"
test -r $name &&echo "you can read this file"
test -w $name &&echo "you can write this file"
test -x $name &&echo "you can exec this file"
exit 0
執(zhí)行結(jié)果
[root@localhost scripts]# sh test.sh
please input file/dir name: n
your input is n
file is not exist!
[root@localhost scripts]# sh test.sh
please input file/dir name: sh01.sh
your input is sh01.sh
file is regulate file
you can read this file
you can write this file
you can exec this file
[root@localhost scripts]# ll sh01.sh
-rwxr--r-- 1 root root 48 03-07 10:15 sh01.sh
測(cè)試命令
[]測(cè)試命令和 test 用法一直,只是使用時(shí)要注意空格符使用
在中括號(hào)內(nèi)的每個(gè)組件又要有空格符分割
在中括號(hào)內(nèi)的變量常量都要使用“”括起來(lái)
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
read -p "please input y/n: " yn
[ -z "$yn" ] &&echo "your input is empty"&&exit 1
[ "$yn" == "y" -o "$yn" == "Y" ]&&echo "it is ok"&&exit 0
[ "$yn" == "n" -o "$yn" == "N" ]&&echo "it is not ok"&&exit 0
echo "you input is $yn .please use input (y/n)"
exit 2
執(zhí)行結(jié)果
[root@localhost scripts]# sh [].sh
please input y/n:
your input is empty
[root@localhost scripts]# sh [].sh
please input y/n: Y
it is ok
[root@localhost scripts]# sh [].sh
please input y/n: A
you input is A .please use input (y/n)
執(zhí)行參數(shù)
可以在執(zhí)行 shell script 時(shí)添加參數(shù)
sh var.sh first second third
$1 $2 $3
其中 first 作為第一個(gè)參數(shù)存儲(chǔ)在$1中,依次類(lèi)推。
幾個(gè)重要的參數(shù):
$#:獲取參數(shù)的數(shù)量
$@:獲取參數(shù)的整體內(nèi)容
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
echo "total parameter number is : $#"
echo "whole parameter is : $@"
echo "1st parameter is : $1"
echo "2st parameter is : $2"
執(zhí)行結(jié)果
[root@localhost scripts]# sh var.sh first second third
total parameter number is : 3
whole parameter is : first second third
1st parameter is : first
2st parameter is : second
if…..then
語(yǔ)法:
If [條件判斷式 ] ;then
待執(zhí)行的 shell 命令
fi
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
read -p "please input y/n: " yn
if [ -z "$yn" ];then
echo "your input is empty"
exit 1
fi
if [ "$yn" == "y" -o "$yn" == "Y" ];then
echo "it is ok"
exit 0
fi
if [ "$yn" == "n" -o "$yn" == "N" ];then
echo "it is not ok"
exit 0
fi
exit 2
執(zhí)行結(jié)果
[root@localhost scripts]# sh ifthen.sh
please input y/n:
your input is empty
[root@localhost scripts]# sh ifthen.sh
please input y/n: Y
it is ok
[root@localhost scripts]# sh ifthen.sh
please input y/n: N
it is not ok
if …else
語(yǔ)法:
If [條件判斷式 ] ;then
待執(zhí)行的 shell 命令
else
待執(zhí)行的 shell 命令
fi
if …elif..else
語(yǔ)法:
If [條件判斷式 ] ;then
待執(zhí)行的 shell 命令
elif[ 條件判斷式 ] ;then
待執(zhí)行的 shell 命令
else
待執(zhí)行的 shell 命令
fi
case …esac
語(yǔ)法:
case$變量 in
“第一個(gè)變量?jī)?nèi)容”)
待執(zhí)行的 shell 命令
;;
“第 n 個(gè)變量?jī)?nèi)容”)
待執(zhí)行的 shell 命令
;;
) è 最后一個(gè)變量?jī)?nèi)容. 代表所有
待執(zhí)行的 shell 命令
;;
esac
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
case $1 in
"hello")
echo " hello how are you!"
;;
"")
echo "you must input parameter!"
;;
*)
echo "parameter is hello,yours is $1"
;;
esac
執(zhí)行結(jié)果
[root@localhost scripts]# sh case.sh
you must input parameter!
[root@localhost scripts]# sh case.sh hello
hello how are you!
[root@localhost scripts]# sh case.sh he
parameter is hello,yours is he
函數(shù)功能
語(yǔ)法:
functionfname()
{
程序段
}
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
function print()
{
if [ -z "$1" ];then
echo "你沒(méi)有輸入?yún)?shù)"
else
echo -n "你輸入的是 $1"
fi
}
function returnval()
{
return 3
}
case $1 in
"one")
print 1
;;
"return")
returnval
echo "return value is $?"
;;
"nopara")
print
;;
esac
執(zhí)行結(jié)果
[root@localhost scripts]# sh function.sh one
你輸入的是 1
[root@localhost scripts]# sh function.sh nopara
你沒(méi)有輸入?yún)?shù)
[root@localhost scripts]# sh function.sh return
return value is 3
函數(shù)后面也可以添加參數(shù)$0記錄函數(shù)名,$1記錄參數(shù)1,依次類(lèi)推
函數(shù)也可以具有返回值,返回值內(nèi)容通過(guò)$?查看
while do done
語(yǔ)法:
while[ 條件表達(dá)式 ] =>只要條件滿(mǎn)足就執(zhí)行
do
程序段
done
舉例
shellscript 文件內(nèi)容:
declare -i sum=0;
declare -i num=0
while [ $num -le 100 ]
do
sum=sum+num
num=num+1
done
echo "總和是: $sum"
執(zhí)行結(jié)果
[root@localhost scripts]# sh while.sh
總和是: 5050
until do done
語(yǔ)法:
while[ 條件表達(dá)式 ] =>只要條件不滿(mǎn)足就執(zhí)行
do
程序段
done
for do done
語(yǔ)法:
for varin con1 con2….
for varin conarr
for (初始值;限制值;執(zhí)行步長(zhǎng))
舉例
shellscript 文件內(nèi)容:
#!/bin/bash
userarr=$(cat /etc/passwd | cut -d ':' -f 1|head -n 5)
for user in $userarr
do
echo "user is : $user"
done
for char in A B C
do
echo "char is $char"
done
sum=0
for ((i=1; i<=$1; i++))
do
sum=$(($sum+$i))
done
echo "1+2..+$1=$sum"
執(zhí)行結(jié)果
[root@localhost scripts]# sh for.sh 100
user is : root
user is : bin
user is : daemon
user is : adm
user is : lp
char is A
char is B
char is C
1+2..+100=5050
語(yǔ)法:sh [-nvx] xxx.sh
選項(xiàng)與參數(shù):
-n:不執(zhí)行,僅檢查語(yǔ)法錯(cuò)誤
-v:在執(zhí)行前,輸出 script 內(nèi)容
-x:將使用到 script 內(nèi)容顯示到屏幕,追蹤主要靠這個(gè)
舉例:
[root@localhost scripts]# sh -x function.sh nopara
+ case $1 in
+ print
+ '[' -z '' ']'
+ echo $'\344\275\240\346\262\241\346\234\211\350\276\223\345\205\245\345\217\202\346\225\260'
你沒(méi)有輸入?yún)?shù)
[root@localhost scripts]# sh -x function.sh one
+ case $1 in
+ print 1
+ '[' -z 1 ']'
+ echo '你輸入的是 1'
你輸入的是 1