A, shell scripts Introduction
(A) Case script and presentation:
#!/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
(B) shell script interpreter:
[root@web01 ~]# echo $SHELL
/bin/bash
[root@web01 ~]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Oct 10 2018 /bin/sh -> bash
Interpreter defaults to bash, if not marked script interpreter, the default call bash.
Note Batch Method 1: ctrl + shift + v, the cursor is moved downward, shitf + a, a first front row #, esc.
Batch Notes Method 2: write content that will be annotated in the following symbol :: << EOF XXX EOF colon represent what not to do.
Notes batch method 3: cat> / dev / null << EOF XXX EOF
(C) shell of implementation:
Method 1: bash, sh
Method 2: / path / script-name or ./script-name
Method 3: source script-name or .script-name
Method 4: sh <script-name or cat scripts-name | sh
Example:
[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]#
Example:
[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
Method 3: source script-name or .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。
(Iv) shell execution:
Parent shell script - external commands - sub-script - the parent shell
Can not call each other between the parent and child shell shell:. If you want to call each other, using the source and point to perform.
shell scripting norms and habits:
① beginning plus interpreter: #! / Bin / bash
② incidental author and copyright information: oldboy at dddd
③ script extensions: .sh
④ script fixed storage location: / server / scripts
⑤ do not use Chinese script.
⑥ pairs of symbols to complete once written. Brackets, both sides need space
⑦ cycle entered once the format is completed.
Second, the basics of variables (chapter book)
the shell variable type variable is not defined. Whether shell variables for easy call.
shell: Environment variables (global variables), common variables (local variables)
shell does not distinguish between types, use the time to distinguish between variable types.
(A) shell variables Category:
Environment variables: global variables, display environment variable: echo $ variable; env; set
Define environment variables: System inherent: PS1, PATH, HOME, UID
method 1
export OLDBOY=1;
Method 2
OLDBOY=1
export OLDBOY
Permanent method of:
Added to / etc / profile;. / Etc / profile
Method 3
declare -x A=1
Cancellation environment variable: unset variables
Environment variable file:
Global File
/etc/profile
/etc/bashrc
User environment variables file
~/.bashrc
~/.bash_profile
Order environment variable in force:
①~/.bash_profile
②~ /.bashrc
③/etc/bashrc
④/etc/profile
Login shell:
First load / etc / profile; ~ / .bash_profile, then load ~ / .bashrc; loading / etc / bashrc again (Commencement reverse order)
Ordinary variables: local variables.
Entry into force of the current user or script.
① string variable
② variable names: letters, numbers, underscores, can not start with a number.
Variable names defined rules: See name known meaning. The first letter, underscore the word.
③ variable content: string,
Single quotes: WYSIWYG.
Without quotation marks, double quotation marks: first resolve the variable or command, and then output.
The contents of double quotation marks can be defined as a whole. Pure numbers do not add quotation marks.
Command variables: anti-quotation marks, brackets
变量名=`ls`
变量名=$(ls)
Ordinary variables Summary:
① Normal string variable defined in the script, as far as possible the contents of the variable double quotes.
② pure digital variable content can not be quoted.
③ the content of the variable as desired output requires single quotation marks.
④ want to get variable value reference command and the command will use the results of anti-quotation marks, or $ ()
⑤$db_t,若变量后面有其他字符连接的时候,就必须给变量加上大括号{},例如$db_t就要改成${db}_t。
⑥ defined variable names have a certain command specification, and to see to know the name of Italy.
⑦ variable definition using the assignment symbol (=), the assignment ends of symbols without spaces.
Three, SHELL variable advanced knowledge and practice (Chapter 4)
(A) shell particular position variable
$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]#
(B) shell process special state variables
$?:获取上一个命令的返回值,返回值为0,表示成功,非0,表示失败。
$$:获取当前执行脚本的进程号。
$!:获取上一个后台工作的进程的进程号。
$_:获取在此前执行命令或者脚本的最后一个参数。
(C) shell variable substring knowledge and practice (variable content)
[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]#
(D) shell variable expansion special knowledge
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]#
Fourth, the shell variable data calculation (Chapter 5)
(A) arithmetic operators:
+,-
*,/,%
**:幂运算,最先计算。
++,--
!,&&,||
<,>,<=
==,!=,=
<<,>>:向左,右移位。
~,|,&,^:按位取反,按位异或,按位与,按位或
=,+=,-=,*=,/=,%=
(B) common programming operation command
只适合整数:
①(())
[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
(C) expr enterprise-class real case Detailed
Whether a judge is an integer:
[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 determine the file extension:
[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 string length is calculated:
[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]#
Five, bash built-in command read core foundation and practice
(A) read Introduction
reading read, read user input.
-p Tips
-t waits for user input.
[root@centos6-kvm3 scripts]# read -t 30 -p "请输入一个数字:" a
请输入一个数字:14
[root@centos6-kvm3 scripts]# echo $a
14
read read function: interaction.
[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
(B) read enterprise applications:
[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]#
Sixth, the conditions of the test compared with shell scripts (Chapter 6)
Common syntax (a) conditional expression
1, conditional expressions six kinds of writing, if, while
Syntax 1: test <test expression>
Syntax 2: [<test expression>] # in both ends of the brackets must have spaces
Syntax 3: [[<test expression>]] # both ends must have a space
Syntax 4: ((test expression)) # both ends will not need space
5 :( command syntax expression)
Syntax 6: command expression
①[]条件表达式
[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]#
(B) conditional expression editor syntax:
1), [<test expression>] && Command 1 Command 2 ||
If the previous expression is successful, then execute the command 1, 2 or execute commands
if [ <测试表达式>]
then
命令1
else
命令2
fi
2) when a lot of command, we can use braces to enclose all command. as follows:
[<Test expression>] && {
Command 1
Command 2
}||{
Command 3
Command 4
}
3), leaving only the successful execution of
[<Test expression>] && {
Command 1
Command 2
Command 3
}
4), leaving only the failure to execute
[<Test expression>] || {
Command 1
Command 2
Command 3
}
(C) document the test expression
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
(Iv) common functional test string expression Description
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
Example:
[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]#
(V) integer expression test
Comparison of the expression used in [] and in the test (()) and [[]] comparison symbols used in the description
-eq == = equal or equal
-ne! Is not equal to equal = Not
-gt> greater than the then Greater
-ge> = greater than or equal equal Greater
-LT-<smaller than the then less
-le <= Less than or equal less equal
Examples
[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表达式只适合符号表达式。
(Vi) test questions: read the interactive mode, to compare the size of two integers.
[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]#
(Vii) the logic test expression
Using an operator in [] and in the test [[]] and (()) using operator instructions
-a && and with
-o || or or
!! Non Not
Example:
[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