shell编程实战学习(2)

shell编程实战学习(2)

一、Shell read的使用

1.1.1 read的使用

  1. read是bash的内置命名,可也通过复制或传参的来获取变量
参数 作用
-p 设置提示信息
-t 设置超时时间
  1. 赋值运算
[root@web01 /server/scripts]# cat read.sh 
#!/bin/bash 
a=9
b=6
#read -p "please input two nmb: " 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@web01 /server/scripts]# sh read.sh
a+b=15
a*b=54
a/b=1
a-b=3
a%b=3
a**b=531441
  1. read读入运算
[root@web01 /server/scripts]# cat read.sh 
#!/bin/bash 

read -p "please input two nmb: " 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))"
  1. 脚本传参运算
[root@web01 /server/scripts]# cat chuancan.sh 
#!/bin/bash 
echo "a+b=$(($1+$2))"
echo "a*b=$(($1*$2))"
echo "a-b=$(($1-$2))"
echo "a/b=$(($1/$2))"
echo "a%b=$(($1%$2))"
echo "a**b=$(($1**$2))"
[root@web01 /server/scripts]# sh chuancan.sh 9 6
a+b=15
a*b=54
a-b=3
a/b=1
a%b=3
a**b=531441
[root@web01 /server/scripts]# sh chuancan.sh 10 5
a+b=15
a*b=50
a-b=5
a/b=2
a%b=0
a**b=100000

二、Shell的条件测试与比较

2.1.1 条件测试语法

条件测试语法 说明
test 这是利用test命令进行测试条件表达式的方法。test命令和“<测试表达式>” 之间至少有一个空格
[] 这是通过[](单中括号)进行条件表达式的方法,和test命令的用法相同 之间至少有一个空格
[[]] 这是通过[[]](双中括号)进行条件表达式的方法,比test,[]更新的语法格式 之间至少有一个空格
(()) 通过(())(双小括号)进行测试条件表达式的方法,一般用于if语句里 不需要空格
  1. test 语法
[root@web01 /server/scripts]# test -f linux && echo 0 || echo 1
1
[root@web01 /server/scripts]# test ! -f linux && echo 0 || echo 1
0
  1. [] 语法
[root@web01 /server/scripts]# [ -f linux  ] && echo 0 || echo 1
1
[root@web01 /server/scripts]# [ ! -f linux  ] && echo 0 || echo 1
0
  1. [[]] 语法
[root@web01 /server/scripts]# [[ -f linux  ]] && echo 0 || echo 1
1
[root@web01 /server/scripts]# [[ ! -f linux  ]] && echo 0 || echo 1
0
  1. (()) 语法
[root@web01 /server/scripts]# ((6 > 5)) &&  echo 0 || echo 1
0
[root@web01 /server/scripts]# ((6 == 5)) &&  echo 0 || echo 1
1
  1. man帮助

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING

       -z STRING
              the length of STRING is zero

       STRING1 = STRING2
              the strings are equal

       STRING1 != STRING2
              the strings are not equal

       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2

       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2
              FILE1 is older than FILE2

       -b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and read permission is granted

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and write permission is granted

       -x FILE
              FILE exists and execute (or search) permission is granted
  1. 使用
[root@web01 /server/scripts]# mkdir linux
[root@web01 /server/scripts]# [ -e linux  ] && echo 0 || echo 1
0
[root@web01 /server/scripts]# [ -d linux  ] && echo 0 || echo 1
0
[root@web01 /server/scripts]# [ -f linux  ] && echo 0 || echo 1
1
[root@web01 /server/scripts]# [ -w linux  ] && echo 0 || echo 1
0
[root@web01 /server/scripts]# [ -L linux  ] && echo 0 || echo 1
1
[root@web01 /server/scripts]# [ -r linux  ] && echo 0 || echo 1
0
  1. 特殊表达式
[ 条件1 ] && {
    命令1     
	命令2     
	命令3 
}
如果条件成立,那么执行三个命令。

if [ 条件1 ]   
then     
	命令1     
	命令2     
	命令3 
fi

[ 条件1 ] || {
    命令1     
	命令2     
	命令3 
}
如果表达式不成立,那么执行三个命令。


[ -L oldboy ] && echo 1 || echo 0

[ 条件1 ] && {
    命令1     
	命令2     
} || { 
	命令3 
}
如果条件成立,那么执行命令1命令2,否则执行命令3。
  1. 字符串比较
符号 作用
-z 字符串长度为空,为真
-n 字符串长度不为空,为真
“字符” = “字符” 两个字符串相等为真
“字符” != “字符” 两个字符串不相等为真
  1. 测试
[root@web01 /server/scripts]# a=linux
[root@web01 /server/scripts]# [ -z "$a" ] && echo 0 || echo 1
1
[root@web01 /server/scripts]# [ -n "$a" ] && echo 0 || echo 1
0
[root@web01 /server/scripts]# [ "$a" = "linux" ] && echo 0 || echo 1
0
[root@web01 /server/scripts]# [ "$a" != "linux" ] && echo 0 || echo 1
1
  1. 判断一个数是否是整数
[root@web01 /server/scripts]# cat read.sh 
#!/bin/bash 
read -p "please input two nmb: " a b
[ -n "$a" -a -n "$b" ] || {
 echo "please input two nmb"
 exit 0
} 
expr $a + 1 &>/dev/null
[ $? -eq 0 ] || { 
  echo "please input two int nmb: "
  exit 0 
} 
expr $b + 1 &>/dev/null
[ $? -eq 0 ] || { 
  echo "please input two int nmb: "
  exit 0 
} 
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@web01 /server/scripts]# cat read.sh 
#!/bin/bash 
read -p "please input two nmb: " a b
[ -n "$a" -a -n "$b" ] || {
 echo "please input two nmb"
 exit 0
} 
expr $a + $b + 9 &>/dev/null
[ $? -eq 0 ] || { 
  echo "please input two int nmb: "
  exit 0 
} 
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@web01 /server/scripts]# sh read.sh
please input two nmb: 
please input two nmb
#输入字符
[root@web01 /server/scripts]# sh read.sh
please input two nmb: k 3
please input two nmb:
#输入两个整数
[root@web01 /server/scripts]# sh read.sh
please input two nmb: 9 6
a+b=15
a*b=54
a/b=1
a-b=3
a%b=3
a**b=531441

11.传参脚本

[root@web01 /server/scripts]# cat chuancan.sh 
#!/bin/bash
[ $# -eq 2 ] ||{
 echo "pelase inpou tow nmb:"
 exit 0
} 

expr $1 + $2 + 5 &>/dev/null
[ $? -eq 0 ] ||{
 echo "pelase inpou tow nmb:"
 exit 1
}
echo "a+b=$(($1+$2))"
echo "a*b=$(($1*$2))"
echo "a-b=$(($1-$2))"
echo "a/b=$(($1/$2))"
echo "a%b=$(($1%$2))"
echo "a**b=$(($1**$2))"

2.1.2 整数二元比较操作符

1.二元整数比较

数学符号表示方式 字母表示方式
> -gt
>= -ge
< -lt
<= -le
= -eq
!= -ne
  1. 例子
#[]
[root@web01 /server/scripts]# [ 1 -ne 2 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ 1 -eq 2 ] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [ 1 -ne 2 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ 1 -le 2 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ 1 -ge 2 ] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [ 1 -gt 2 ] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [ 1 -lt 2 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ 1 \< 2 ] && echo 1 || echo 0 #注意如果想用数学比较符 比较符需要转移
1

#[[]]
[root@web01 /server/scripts]# [[ 1 < 2 ]] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [[ 3 < 2 ]] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [[ 3 = 2 ]] && echo 1 || echo 0
[root@web01 /server/scripts]# [[ 3 -eq 2 ]] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [[ 3 -ne 2 ]] && echo 1 || echo 0
1
#(())
[root@web01 /server/scripts]# ((3 > 2)) && echo 1 || echo 0
1
[root@web01 /server/scripts]# ((3 == 2)) && echo 1 || echo 0
0
[root@web01 /server/scripts]# ((3 -ne 2)) && echo 1 || echo 0  #报错不能使用二元整数表达式
-bash: ((: 3 -ne 2: syntax error in expression (error token is "2")
0
[root@web01 /server/scripts]# ((3 -eq 2)) && echo 1 || echo 0
-bash: ((: 3 -eq 2: syntax error in expression (error token is "2")
0

2.1.3 逻辑操作符

适用范围test和[]操作符 适用范围[[]]和(()) 说明
-a && and 与,两端都为真则真
-o || or 或 两端为或有一端为真,则真
not 非 相反为真
  1. 例子
#-a
[root@web01 /server/scripts]# [ -f /etc/profile -a 6 -gt 5 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ -f /etc/profil -a 6 -gt 5 ] && echo 1 || echo 0
0
#-o
[root@web01 /server/scripts]# [ -f /etc/profil -o 6 -gt 5 ] && echo 1 || echo 0
1
#!
[root@web01 /server/scripts]# [ ! -f /etc/profil -o 6 -gt 5 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [ ! -f /etc/profil -o 6 -lt 5 ] && echo 1 || echo 0
1
[root@web01 /server/scripts]# [[  -f /etc/profil && 6 -lt 5 ]] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [[  -f /etc/profil || 6 -lt 5 ]] && echo 1 || echo 0
0
[root@web01 /server/scripts]# [[ ! -f /etc/profil || 6 -lt 5 ]] && echo 1 || echo 0
1
  1. 例子
[root@web01 /server/scripts]# cat diff.sh 
#!/bin/bash
[ $1 -eq 1 -o $1 -eq 2 ] &&{ 
  echo "$1"
} || {

 echo "input error"
 exit 1
} 
[root@web01 /server/scripts]# sh diff.sh 1
1
[root@web01 /server/scripts]# sh diff.sh 2
2
[root@web01 /server/scripts]# sh diff.sh 3
input error
##########################################
[root@web01 /server/scripts]# cat diff1.sh 
#!/bin/bash
expr $1 + $2 + 3 &>/dev/null
[ $? -eq 0 ] ||{
 echo "Usage:$0 num1 num2"
 exit 0
}
[ $1 -gt $2 ] && {
  echo "$1 > $2"
  exit 1
}

[ $1 -lt $2 ] && {
  echo "$1 < $2"
  exit 2
}
 
[ $1 -eq $2 ] && {
  echo "$1 = $2"
}
[root@web01 /server/scripts]# sh diff1.sh 4 3
4 > 3
[root@web01 /server/scripts]# sh diff1.sh 4 5
4 < 5
[root@web01 /server/scripts]# sh diff1.sh 4 4
4 = 4
[root@web01 /server/scripts]# sh diff1.sh 4 k
Usage:diff1.sh num1 num2
[root@web01 /server/scripts]# sh diff1.sh k k
Usage:diff1.sh num1 num2
[root@web01 /server/scripts]# sh diff1.sh k 4
Usage:diff1.sh num1 num2

2.1.4 read 菜单编写

  1. 下载菜单编写
[root@web01 /server/scripts]# cat read_install.sh 
#!/bin/bash
num1 () { 
cat <<END
*****************************
1. PHP    install
2. Mysql  install
3. Nginx  install
4. Tomcat install
5. exit
please input server num:
*****************************
END
}


red () {
read -p "please input num: " a 
}

list () { 
[ $a -eq 1 ] && {
  echo "PHP installed...... "
  echo "PHP intsall is ok"
  exit 1
}
[ $a -eq 2 ] && {
  echo "Nginx installed...... "
  echo "Nginx intsall is ok"
  exit 2
}
[ $a -eq 3 ] && {
  echo "Mysql installed...... "
  echo "Mysql intsall is ok"
  exit 3
}
[ $a -eq 4 ] && {
  echo "Tomcat installed...... "
  echo "Tomcat intsall is ok"
  exit 4
}
[ $a -eq 5 ] && exit 5
[[ ! $a =~ [1-3] ]] && {   #这里就是用到了[[]]的通配符匹配的用法。即a是否为1,2,3
  echo "imput error Usage:{1|2|3|4|5}"
}
}
while true
do
 num1 
 red
 list
 num1
 red
 list
done
###########################################
[root@web01 /server/scripts]# sh read_install.sh
*****************************
1. PHP    install
2. Mysql  install
3. Nginx  install
4. Tomcat install
5. exit
please input server num:
*****************************
please input num: 3
Mysql installed...... 
Mysql intsall is ok
[root@web01 /server/scripts]# sh read_install.sh
*****************************
1. PHP    install
2. Mysql  install
3. Nginx  install
4. Tomcat install
5. exit
please input server num:
*****************************
please input num: 4
Tomcat installed...... 
Tomcat intsall is ok
[root@web01 /server/scripts]# sh read_install.sh
*****************************
1. PHP    install
2. Mysql  install
3. Nginx  install
4. Tomcat install
5. exit
please input server num:
*****************************
please input num: 9
imput error Usage:{1|2|3|4|5}
*****************************
1. PHP    install
2. Mysql  install
3. Nginx  install
4. Tomcat install
5. exit
please input server num:
*****************************
please input num: 5

猜你喜欢

转载自www.cnblogs.com/woaiyunwei/p/12942088.html