一、shell腳本介紹
(一)腳本案例及介紹:
#!/bin/bash
LOG_DIR=/var/log
ROOT_UID=0
if ["$UID -ne "$ROOT_UID"]
then
echo "must be root run this script."
exit 1
fi
cd $ LOG_DIR || {
echo "cannot change to necessary directory"
exit 1
}
cat /dev/null>message && {
echo "logs cleaned up."
exit 0
}
echo "logs cleaned fail."
exit 1
(二)shell腳本解釋器:
[root@web01 ~]# echo $SHELL
/bin/bash
[root@web01 ~]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Oct 10 2018 /bin/sh -> bash
解釋器默認為bash,如果腳本中不標明解釋器,默認呼叫bash,
批量注釋方法1:ctrl+shift+v,游標向下移動,shitf+a,第一行前#,esc,
批量注釋方法2:將要注釋的內容寫在下面符號內::<<EOF XXX EOF.冒號表示什么的都不做,
批量注釋方法3:cat >/dev/null<<EOF XXX EOF
(三)shell執行方法:
方法1:bash,sh
方法2:/path/script-name 或者./script-name
方法3:source script-name 或者.script-name
方法4:sh<script-name 或者 cat scripts-name | sh
實體:
[root@web01 scripts01]# bash test.sh
i am oldboy teacher.
[root@web01 scripts01]# sh test.sh
i am oldboy teacher.
[root@web01 scripts01]# ./test.sh
-bash: ./test.sh: Permission denied
[root@web01 scripts01]# /server/scripts01/test.sh
-bash: /server/scripts01/test.sh: Permission denied
[root@web01 scripts01]# ls -l test.sh
-rw-r--r-- 1 root root 127 Jan 18 23:12 test.sh
[root@web01 scripts01]# chmod +x test.sh
[root@web01 scripts01]# /server/scripts01/test.sh
i am oldboy teacher.
[root@web01 scripts01]# sh < test.sh
i am oldboy teacher.
[root@web01 scripts01]# cat test.sh | sh
i am oldboy teacher.
[root@web01 scripts01]#
實體:
[root@web01 scripts01]# chkconfig --list | grep 3:on | awk '{print "chkconfig", $1,"off"}'
chkconfig crond off
chkconfig network off
chkconfig rsyslog off
chkconfig sshd off
[root@web01 scripts01]# chkconfig --list | grep 3:on | awk '{print "chkconfig", $1,"off"}' | bash
[root@web01 scripts01]# chkconfig --list | grep 3:on
方法3:source script-name 或者.script-name
[root@web01 scripts01]# cat test1.sh
user=`whoami`
[root@web01 scripts01]# sh test1.sh
[root@web01 scripts01]# echo $user
[root@web01 scripts01]#
# sh 相當于在當前shell下新開啟一個子shell,所以echo $user,在當前開啟shell下執行,
[root@web01 scripts01]# source test1.sh
[root@web01 scripts01]# echo $user
root
[root@web01 scripts01]#
#source 相當于在當前shell下執行,
父shell
子shell
使用source 和.來執行腳本,相當于在一個shell執行腳本,可以相互呼叫,
使用bash或者sh執行腳本,開啟一個新的shell,或者開啟子shell,
[root@web01 scripts01]# vim 1.sh
sh test1.sh
echo $user
[root@web01 scripts01]# sh 1.sh
[root@web01 scripts01]# cat 1.sh
sh test1.sh
echo $user
[root@web01 scripts01]# cat 1.sh
. ./test1.sh
echo $user
[root@web01 scripts01]# sh 1.sh
root
使用source 和.可以相互呼叫父shell和子shell,
(四)shell執行程序:
父shell腳本-外部命令-子腳本-父shell
父shell和子shell之間不能相互呼叫:如果希望相互呼叫,使用source和點.來執行,
shell腳本編程規范和習慣:
①開頭加解釋器:#!/bin/bash
②附帶作者及著作權資訊:oldboy at dddd
③腳本擴展名:.sh
④腳本存放固定位置:/server/scripts
⑤腳本中不使用中文,
⑥成對的符號一次書寫完成,中括號,兩邊需空格
⑦回圈格式一次性輸入完成,
二、變數的基礎知識(書本第三章)
shell中變數中不定義變數型別,shell變數是否為了方便呼叫,
shell變數:環境變數(全域變數),普通變數(區域變數)
shell 不區分型別,使用的時候區分變數型別,
(一)shell變數分類:
環境變數:全域變數,顯示環境變數:echo $變數;env;set
定義環境變數:系統固有:PS1,PATH,HOME,UID
方法1
export OLDBOY=1;
方法2
OLDBOY=1
export OLDBOY
永久生效的方法:
添加至/etc/profile ; . /etc/profile
方法3
declare -x A=1
取消環境變數:unset 變數
環境變數的檔案:
全域檔案
/etc/profile
/etc/bashrc
用戶環境變數檔案
~/.bashrc
~/.bash_profile
環境變數生效的的順序:
①~/.bash_profile
②~ /.bashrc
③/etc/bashrc
④/etc/profile
登錄shell:
先加載/etc/profile ;/.bash_profile,然后加載/.bashrc ;再次加載/etc/bashrc(生效順序相反)
普通變數:區域變數,
當前用戶或者腳本中生效,
①字串變數
②變數名:字母,數字,下劃線,不能以數字開頭,
變數名定義規則:見名知意,首字母,下劃線連接單詞,
③變數內容:字串,
單引號:所見即所得,
不用引號,雙引號:先決議變數或者命令,然后輸出,
雙引號可以把要定義的內容作為一個整體,純數字不加引號,
命令變數:反引號,括號
變數名=`ls`
變數名=$(ls)
普通變數總結:
①在腳本中定義普通字串變數,盡量把變數的內容使用雙引號,
②純數字的變數內容可以不加引號,
③希望變數的內容原樣輸出需要加單引號,
④希望變數值參考命令并獲取命令的結果就用反引號或者$()
⑤$db_t,若變數后面有其他字符連接的時候,就必須給變數加上大括號{},例如$db_t就要改成${db}_t,
⑥變數名的定義要有一定的命令規范,并且要見名知意,
⑦變數定義使用賦值符號(=),賦值符號兩端不要有空格,
三、SHELL變數知識進階與實踐(4章)
(一)shell特殊位置變數
$0:獲取腳本的名字,如果腳本前跟著路徑的話,那就獲取路徑加上腳本名字,
企業應用:一般在腳本最后,使用$0獲取腳本的路徑和名字給用戶,
$n:獲取腳本后的第n個引數,n大于9以后,數字需要用大括號括起來,
企業應用:腳本中,提取第n個引數,
$#:腳本后所有引數的個數,
企業應用:判斷引數個數,
$*:獲取shell腳本中所有的引數,所有單數是一個整體:"$1,$2,$3"
$@:獲取腳本的所有引數,每個引數是一個整體:"$1","$2","$3"
當需要接收腳本后所有引數,但是又不知道個數的時候,使用$*,$#
兩者區別:
[root@centos6-kvm3 scripts]# cat test.sh
#!/bin/bash
for arg in "$*"
do
echo $arg
done
echo ------
for arg1 in "$@"
do
echo $arg1
done
echo $#
[root@centos6-kvm3 scripts]# bash test.sh "i am" oldboy teacher.
i am oldboy teacher.
------
i am
oldboy
teacher.
3
[root@centos6-kvm3 scripts]#
(二)shell行程特殊狀態變數
$?:獲取上一個命令的回傳值,回傳值為0,表示成功,非0,表示失敗,
$$:獲取當前執行腳本的行程號,
$!:獲取上一個后臺作業的行程的行程號,
$_:獲取在此前執行命令或者腳本的最后一個引數,
(三)shell變數子串知識及實踐(變數內容)
[root@centos6-kvm3 scripts]# oldboy="i am oldboy"
[root@centos6-kvm3 scripts]# echo ${oldboy}
i am oldboy
${#變數}:獲取變數字符個數,
[root@centos6-kvm3 scripts]# echo ${#oldboy}
11
[root@centos6-kvm3 scripts]# echo ${oldboy}|wc -L
11
計算變數字符個數方法2:
[root@centos6-kvm3 scripts]# expr length "$oldboy"
11
計算變數字符個數方法3:
[root@centos6-kvm3 scripts]# echo $oldboy| awk '{print length }'
11
[root@centos6-kvm3 scripts]# echo $oldboy| awk '{print length($0) }'
11
[root@centos6-kvm3 scripts]# echo $oldboy| awk '{print length($1) }'
1
獲取變數第二個引數后引數:
[root@centos6-kvm3 scripts]# echo ${oldboy:2}
am oldboy
[root@centos6-kvm3 scripts]# echo ${oldboy:2:2}
am
[root@centos6-kvm3 scripts]#
${引數#字串}:匹配開頭,洗掉最短匹配,
[root@centos6-kvm3 scripts]# OLDBOY=abcABC12345ABCabc
[root@centos6-kvm3 scripts]# echo ${OLDBOY}
abcABC12345ABCabc
[root@centos6-kvm3 scripts]# echo ${OLDBOY#a*C}
12345ABCabc
${引數##字串}:匹配開頭,洗掉最長匹配,
[root@centos6-kvm3 scripts]# echo ${OLDBOY##a*C}
abc
${引數%字串}:匹配結尾,洗掉最短匹配,
[root@centos6-kvm3 scripts]# echo ${OLDBOY%a*c}
abcABC12345ABC
${引數%%字串}:匹配結尾,洗掉最長匹配,
[root@centos6-kvm3 scripts]# echo ${OLDBOY%%a*c}
[root@centos6-kvm3 scripts]#
${變數/part/string}:使用string替換part第一個匹配項,
[root@centos6-kvm3 scripts]# oldboy="i am oldboy oldboy"
[root@centos6-kvm3 scripts]# echo ${oldboy/oldboy/oldgirl}
i am oldgirl oldboy
${變數//part/string}:使用string替換part所有匹配項,
[root@centos6-kvm3 scripts]# echo ${oldboy//oldboy/oldgirl}
i am oldgirl oldgirl
[root@centos6-kvm3 scripts]#
(四)shell特殊變數擴展知識
result=${變數:-word}:當變數為空時候,將word賦值給result,冒號可以省略,
[root@centos6-kvm3 scripts]# result=${test:-UNSET}
[root@centos6-kvm3 scripts]# echo $result
UNSET
[root@centos6-kvm3 scripts]# echo $test
企業應用:
[root@centos6-kvm3 scripts]# find ${path:-/tmp} -name "*.log" -mtime +7| xargs rm -f
[root@centos6-kvm3 scripts]#
result=${變數:=word},變數為空時候,work復制給result,同時復制給變數,
[root@centos6-kvm3 scripts]# result=${test:=UNSET}
[root@centos6-kvm3 scripts]# echo ${result}
UNSET
[root@centos6-kvm3 scripts]# echo ${test}
UNSET
[root@centos6-kvm3 scripts]#
${變數:?word}:當變數為空時候,提示word,
[root@centos6-kvm3 scripts]# result=${test1:?變數為空}
-bash: test1: 變數為空
[root@centos6-kvm3 scripts]# echo $result
UNSET
[root@centos6-kvm3 scripts]# echo $test1
[root@centos6-kvm3 scripts]# test1=oldboy
[root@centos6-kvm3 scripts]# result=${test1:?變數為空}
[root@centos6-kvm3 scripts]# echo $result
oldboy
[root@centos6-kvm3 scripts]#
${變數:+word}:如果前面變數為空,什么不做,如果不為空,進行覆寫,
[root@centos6-kvm3 scripts]# result1=${test2:+wordk}
[root@centos6-kvm3 scripts]# echo ${result1}
[root@centos6-kvm3 scripts]# echo ${test2}
[root@centos6-kvm3 scripts]# test2=2
[root@centos6-kvm3 scripts]# result1=${test2:+wordk}
[root@centos6-kvm3 scripts]# echo ${result1}
wordk
[root@centos6-kvm3 scripts]# echo ${test2}
2
[root@centos6-kvm3 scripts]#
四、shell變數的資料計算(5章)
(一)算數運算子:
+,-
*,/,%
**:冪運算,最先計算,
++,--
!,&&,||
<,>,<=
==,!=,=
<<,>>:向左,右移位,
~,|,&,^:按位取反,按位異或,按位與,按位或
=,+=,-=,*=,/=,%=
(二)編程常見運算命令
只適合整數:
①(())
[root@centos6-kvm3 ~]# i=$a+1
[root@centos6-kvm3 ~]# echo $i
1+1
[root@centos6-kvm3 ~]# echo $((a+3))
4
[root@centos6-kvm3 ~]# echo $((2**3))
8
[root@centos6-kvm3 ~]# echo $((1+2**3-5%3))
7
[root@centos6-kvm3 ~]# ((i++))
[root@centos6-kvm3 ~]# echo $i
3
②let
[root@centos6-kvm3 ~]# a=1
[root@centos6-kvm3 ~]# i=$a+1
[root@centos6-kvm3 ~]# let i=$a+1
[root@centos6-kvm3 ~]# echo $i
2
③expr
[root@centos6-kvm3 ~]# expr 2 + 3
5
[root@centos6-kvm3 ~]# expr 2*2
2*2
[root@centos6-kvm3 ~]# expr 2 * 2
expr: syntax error
[root@centos6-kvm3 ~]# expr 2 \* 2
4
④$[]
[root@centos6-kvm3 ~]# echo $[2-3]
-1
[root@centos6-kvm3 ~]# echo $[1+3]
4
既適合整數,又適合小數:
①bc
[root@centos6-kvm3 ~]# bc
1+2
3
2-1
1
[root@centos6-kvm3 ~]# echo 1.1+2| bc
3.1
②awk
[root@centos6-kvm3 ~]# echo 2.1 1.4| awk '{print $1*$2}'
2.94
[root@centos6-kvm3 ~]# echo 2.1 1.4| awk '{print $1-$2}'
0.7
(三)expr的企業級實戰案例詳解
判斷一個是否為整數:
[root@centos6-kvm3 ~]# expr 2 + 3
5
[root@centos6-kvm3 ~]# expr 2 + a
expr: non-numeric argument
oot@centos6-kvm3 ~]# echo $?
2
[root@centos6-kvm3 ~]# a=2
[root@centos6-kvm3 ~]# expr 2 + $a &>/dev/null
[root@centos6-kvm3 ~]# echo $?
0
[root@centos6-kvm3 ~]# a=oldboy
[root@centos6-kvm3 ~]# expr 2 + $a &>/dev/null
[root@centos6-kvm3 ~]# echo $?
2
[root@centos6-kvm3 ~]#
判斷引數是否為整數:
[root@centos6-kvm3 scripts]# cat judge.sh
#!/bin/bash
expr 2 + $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "$1 is 整數"
else
echo "$1 is not 整數"
fi
[root@centos6-kvm3 scripts]# sh judge.sh 4
4 is 整數
[root@centos6-kvm3 scripts]# sh judge.sh j
j is not 整數
[root@centos6-kvm3 scripts]#
expr判斷檔案擴展名:
[root@centos6-kvm3 scripts]# cat judge1.sh
#!/bin/bash
expr "$1" : ".*\.txt" &>/dev/null
if [ $? -eq 0 ]
then
echo "$1 is 文本"
else
echo "$1 is not 文本"
fi
[root@centos6-kvm3 scripts]# sh judge1.sh old.txt
old.txt is 文本
[root@centos6-kvm3 scripts]# sh judge1.sh old.log
old.log is not 文本
[root@centos6-kvm3 scripts]#
expr計算字串長度:
[root@centos6-kvm3 scripts]# oldboy="i am oldboy"
[root@centos6-kvm3 scripts]# echo ${#oldboy}
11
[root@centos6-kvm3 scripts]# expr length "$oldboy"
11
[root@centos6-kvm3 scripts]#
五、bash內置核心命令read基礎及實踐
(一)read介紹
read 讀入,讀取用戶輸入,
-p 提示
-t 等待用戶輸入的時間,
[root@centos6-kvm3 scripts]# read -t 30 -p "請輸入一個數字:" a
請輸入一個數字:14
[root@centos6-kvm3 scripts]# echo $a
14
read 讀入的作用:互動,
[root@centos6-kvm3 scripts]# vim test5.sh
#!/bin/bash
a=6
b=2
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@centos6-kvm3 scripts]#
[root@centos6-kvm3 scripts]# sh test5.sh
a-b=4
a+b=8
a*b=12
a/b=3
a**b=36
a%b=0
[root@centos6-kvm3 scripts]# vim test5.sh
#!/bin/bash
read -p "請輸入兩個引數:" a b
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@centos6-kvm3 scripts]# sh test5.sh
請輸入兩個引數:4 5
a-b=-1
a+b=9
a*b=20
a/b=0
a**b=1024
a%b=4
[root@centos6-kvm3 scripts]# vim test5.sh
#!/bin/bash
a=$1
b=$2
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@centos6-kvm3 scripts]#
[root@centos6-kvm3 scripts]#
[root@centos6-kvm3 scripts]# sh test5.sh 5 9
a-b=-4
a+b=14
a*b=45
a/b=0
a**b=1953125
a%b=5
(二)read 的企業應用:
[root@centos6-kvm3 scripts]# cat select.sh
#!/bin/bash
cat << EOF
1.install lamp
2.install lnmp
3.exit
EOF
read -p "請輸入一個序號:" num
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0{1|2|3}"
exit 1
fi
if [ $num -eq 1 ]
then
echo "install lamp..."
elif [ $num -eq 2 ]
then
echo "install lnmp ..."
elif [ $num -eq 3 ]
then
echo "bye..."
exit
else
echo "usage:$0{1|2|3}"
exit 1
fi
[root@centos6-kvm3 scripts]# sh select.sh
1.install lamp
2.install lnmp
3.exit
請輸入一個序號:a
usage:select.sh{1|2|3}
[root@centos6-kvm3 scripts]# sh select.sh
1.install lamp
2.install lnmp
3.exit
請輸入一個序號:4
usage:select.sh{1|2|3}
[root@centos6-kvm3 scripts]# sh select.sh
1.install lamp
2.install lnmp
3.exit
請輸入一個序號:3
bye...
[root@centos6-kvm3 scripts]# sh select.sh
1.install lamp
2.install lnmp
3.exit
請輸入一個序號:2
install lnmp ...
[root@centos6-kvm3 scripts]#
六、shell腳本的條件測驗與比較(6章)
(一)條件運算式的常見語法
1、條件運算式6種寫法,if,while
語法1:test<測驗運算式>
語法2:[ <測驗運算式>] #中括號兩端必須要有空格
語法3:[[<測驗運算式>]] #兩端必須要有空格
語法4:((測驗運算式)) #兩端必不需要空格
語法5:(命令運算式)
語法6:命令運算式
①[]條件運算式
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc/host1 ] && echo 0 || echo 1
1
②test條件運算式:test和[]功能相同
[root@centos6-kvm3 scripts]# test -e /etc/host1 && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# test -e /etc/hosts && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# # [] == test
③[[]]雙中括號條件運算式,兩邊需要空格
[root@centos6-kvm3 scripts]# [[ -e /etc/host1 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [[ -e /etc/hosts ]] && echo 0 || echo 1
0
④雙括號條件運算式,兩邊需要空格
[root@centos6-kvm3 scripts]# (( -e /etc/hosts )) && echo 0 || echo 1
-bash: ((: -e /etc/hosts : division by 0 (error token is "/hosts ")
1
⑤雙括號一般用于計算:
[root@centos6-kvm3 scripts]# ((3>5)) && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# ((3<5)) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# #(())用于計算
⑥expr 運算式:使用括號
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + a &>/dev/null) && echo 0 || echo 1
1
⑦expr 運算式:使用反引號
[root@centos6-kvm3 scripts]# `expr 1 + a &>/dev/null` && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# `expr 1 + 2 &>/dev/null` && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]#
(二)條件運算式的編輯語法:
1)、[<測驗運算式>] && 命令1 ||命令2
如果前面運算式成功,那么執行命令1,否則執行命令2
if [ <測驗運算式>]
then
命令1
else
命令2
fi
2)、當命令很多的時候,我們可以使用大括號把所有命令括起來,如下:
[ <測驗運算式> ] && {
命令1
命令2
}||{
命令3
命令4
}
3)、只保留執行成功的
[ <測驗運算式>] &&{
命令1
命令2
命令3
}
4)、只保留執行失敗的
[<測驗運算式>] || {
命令1
命令2
命令3
}
(三)檔案測驗運算式
man test
-d:檔案為目錄且存在
[root@centos6-kvm3 scripts]# [ -d /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [ -d /etc ] && echo 0 || echo 1
0
-f:判斷為檔案且存在
[root@centos6-kvm3 scripts]# [ -f /etc/hosts ] && echo 0 || echo 1
0
-e:判斷存在,為目錄或者檔案
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc ] && echo 0 || echo 1
0
-r:判斷檔案為可讀:
[root@centos6-kvm3 scripts]# [ -r /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# ll /etc/hosts
-rw-r--r--. 2 root root 352 Nov 19 2018 /etc/hosts
-x:判斷檔案為可執行:
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# chmod +x /etc/hosts
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
0
-s:判斷檔案大小不為0:
[root@centos6-kvm3 scripts]# [ -s /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# touch oldboy.log
[root@centos6-kvm3 scripts]# [ -s oldboy.log ] && echo 0 || echo 1
1
應用示例:crond
[root@centos6-kvm3 scripts]# cat /etc/init.d/crond
(四)字串測驗運算式的常見功能說明
n:not zero ,[-n "字串" ] 字串長度不為0,運算式為真,
z:zero,[-z "字串" ] 字串長度為0,運算式為真,
["字串1"==“字串2”] 兩個字串相同為真,
[“字串1”!=“字串2”] 兩個字串不相同為真,
注意:
1、字串就用雙引號,
2、等號可以用一個或者兩個,
3、等號兩端必須要有空格,
[root@centos6-kvm3 scripts]# [ -n "oldboy" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ -z "oldboy" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# char="oldboy"
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# unset char
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd"=="ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" == "ff" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd" == "dd" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "dd" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]#
實體應用:
cat /etc/init.d/crond
cat /etc/init.d/network
實體:
[root@centos6-kvm3 scripts]# cat select1.sh
#!/bin/bash
cat << EOF
1.install lamp
2.install lnmp
3.exit
EOF
read -p "請輸入一個序號:" num
[ -z "$num" ] && exit 1 #判斷內容是否為空
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0{1|2|3}"
exit 1
fi
if [ $num -eq 1 ]
then
echo "install lamp..."
elif [ $num -eq 2 ]
then
echo "install lnmp ..."
elif [ $num -eq 3 ]
then
echo "bye..."
exit
else
echo "usage:$0{1|2|3}"
exit 1
fi
[root@centos6-kvm3 scripts]#
(五)整數測驗運算式
在[]及test中使用的比較運算式 在(())和[[]]中使用的比較符號 說明
-eq ==或者= 等于equal
-ne != 不等于not equal
-gt > 大于greater then
-ge >= 大于等于greater equal
-lt < 小于 less then
-le <= 小于等于 less equal
實體
[root@centos6-kvm3 ~]# [ 2 -eq 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -gt 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -lt 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 > 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 \> 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 > 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -gt 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -lt 3 ]] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# (( 2 -lt 3 )) && echo 0 || echo 1
-bash: ((: 2 -lt 3 : syntax error in expression (error token is "3 ")
1
[root@centos6-kvm3 ~]# (( 2 > 3 )) && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# (( 2 < 3 )) && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# test 2 -gt 3 && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# test 2 -lt 3 && echo 0 || echo 1
0
[root@centos6-kvm3 ~]#
總結:
1、雙中括號中使用 字母運算式,
2、雙括號中不適合字母運算式,只適合符號運算式,
3、test運算式只適合符號運算式,
(六)測驗題:使用read的互動方式,來比較兩個整數的大小,
[root@centos6-kvm3 scripts]# cat test3.sh
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
echo "請輸入兩個整數,"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "請輸入兩個整數,"
exit 2
}
[ $a -lt $b ] && {
echo "$a小于$b."
exit 0
}
[ $a -gt $b ] && {
echo "$a大于$b."
exit 0
}
[ $a -eq $b ] && {
echo "$a等于$b."
exit 0
}
[root@centos6-kvm3 scripts]#
================
使用if陳述句:
[root@centos6-kvm3 scripts]# cat test4.sh
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
echo "請輸入兩個整數,"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "請輸入兩個整數,"
exit 2
}
if [ $a -lt $b ]
then
echo "$a小于$b."
elif [ $a -gt $b ]
then
echo "$a大于$b."
else
echo "$a等于$b."
fi
[root@centos6-kvm3 scripts]#
(七)邏輯測驗運算式
在[]和test中使用運算子 在[[]]和(())中使用運算子 說明
-a && and 與
-o || or 或
! ! not 非
實體:
[root@centos6-kvm3 scripts]# [ 1 -eq 1 -a -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -a -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -o -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] -o [ -f /etc/hosts ] && echo 1 || echo 0
-bash: [: too many arguments
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] || [ -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] && [ -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 || -f /etc/hosts ]] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 && -f /etc/hosts ]] && echo 1 || echo 0
0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/43565.html
標籤:其他
下一篇:模塊二、shell腳本邏輯結構
