模塊一:shell 腳本基礎

1、shell腳本介紹web

(一)腳本案例及介紹: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腳本解釋器:express

[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。vim

批量註釋方法2:將要註釋的內容寫在下面符號內::<<EOF XXX EOF.冒號表示什麼的都不作。centos

批量註釋方法3:cat >/dev/null<<EOF XXX EOFbash

(三)shell執行方法:less

方法1:bash,shssh

方法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

⑤腳本中不使用中文。

⑥成對的符號一次書寫完成。中括號,兩邊需空格

⑦循環格式一次性輸入完成。

2、變量的基礎知識(書本第三章)

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。

⑥變量名的定義要有必定的命令規範,而且要見名知意。

⑦變量定義使用賦值符號(=),賦值符號兩端不要有空格。

3、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]#

4、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]#

5、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]#

6、shell腳本的條件測試與比較(6章)

(一)條件表達式的常見語法

一、條件表達式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」] 兩個字符串不相同爲真。
注意:
一、字符串就用雙引號。
二、等號能夠用一個或者兩個。
三、等號兩端必需要有空格。

[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 ~]# 
總結:
一、雙中括號中使用 字母表達式。
二、雙括號中不適合字母表達式,只適合符號表達式。
三、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
相關文章
相關標籤/搜索