主頁 >  其他 > 模塊一:shell 腳本基礎

模塊一:shell 腳本基礎

2020-09-15 06:37:09 其他

一、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

標籤:其他

上一篇:高通量計算框架HTCondor(六)——拾遺

下一篇:模塊二、shell腳本邏輯結構

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more