模块一:shell 脚本基础

一、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
posted @ 2020-01-29 00:17  cuiyongchao007  阅读(206)  评论(0编辑  收藏  举报