Linux随笔4

  • 数值测试
    有6种数值比较测试可供使用。
    如果两数相等,-eq返回真;
    如果不相等,-ne返回真;
    -lt与-gt分别用于测试两个数的小于与大于关系。
    如果要测试小于或者等于关系,可以使用-le;
    -ge测试大于或等于的关系。
[root@localhost tmp]# cat numberguess.sh 
#!/bin/bash
MAX=50
guess=-1
let answer=($RANDOM % MAX)
let answer+=1
ceiling=$MAX
floor=0
guesses=0

while [ "$guess" -ne "$answer" ]
do
    echo "The magic number is between $floor and $ceiling ."
    echo -en "Make your guess: "
    read guess
    guesses=`expr $guesses + 1`
    if [ "$guess" -lt "$answer" ]; then
        echo "$guess is too low"
        if [ "$guess" -gt "$floor"  ]; then
        floor=`expr $guess + 1`
        fi
    fi
    if [ "$guess" -gt "$answer" ];then
        echo "$guess is too high"
        if [ "$guess" -lt "$ceiling" ]; then
        ceiling=`expr $guess - 1`
        fi
    fi
done
echo "Your got it in $guesses guesses!"
[root@localhost tmp]# 
  • 组合测试
    可以使用&&和||操作符来将测试组合起来。他们分别实现测试的逻辑与和逻辑或。要测试一个文件是否可读且大小不为0,可以组合-r和-s测试。在本例中,哪个测试失败都没有关系;除非两个条件都为真,否则计算文件文件的md5sum就没有意义。/etc/hosts通常可读且不为空,所以下面的脚本默认参数为/etc/hosts。 /etc/shadow通常也不为空,但除非是root用户或者在shadow组中,否则它是不可读的,所以不可能计算它的md5sum。
[root@localhost tmp]# cat md5-if-possible.sh 
#!/bin/bash
filename=${1:-/etc/hosts}

if [ -r "$filename" ] && [ -s "$filename" ]; then
    md5sum $filename
else
    echo "$filename cannot be processed"
fi
#Show the file if possible
ls -ld $filename 2>/dev/null
[root@localhost tmp]# 
[root@localhost tmp]# ./md5-if-possible.sh /etc/shadow
ed6adcb3ff9b0e2f0d48f4b4b9297fc4  /etc/shadow
----------. 1 root root 955 730 10:41 /etc/shadow
[root@localhost tmp]# ./md5-if-possible.sh /etc/hosts
54fb6627dbaa37721048e4549db3224d  /etc/hosts
-rw-r--r--. 2 root root 158 85 16:07 /etc/hosts
[root@localhost tmp]#

可以通过首先执行速度最快或者最可能失败的测试来提高脚本的运行速度。
短路语句:

[root@localhost tmp]# cat readable-and.sh 
#!/bin/bash
filename=${1:-/etc/hosts}

[ -r $filename ] && echo "$filename is readable"
[root@localhost tmp]# 
[root@localhost tmp]# ./readable-and.sh /etc/hosts
/etc/hosts is readable
[root@localhost tmp]# ./readable-and.sh /etc/shadow
/etc/shadow is readable
[root@localhost tmp]# 

短路语句举例:

[root@localhost tmp]# wc -l /etc/hosts || echo "wc failed to read /etc/hosts"
2 /etc/hosts
[root@localhost tmp]# wc -l /etc/hosts.bak || echo "wc failed to read /etc/hosts.bak"
wc: /etc/hosts.bak: 没有那个文件或目录
wc failed to read /etc/hosts.bak
[root@localhost tmp]# wc -l /etc/hosts| grep "^20" && echo "/etc/hosts is a 20 line file."
[root@localhost tmp]# wc -l /etc/hosts| grep "^20" || echo "/etc/hosts is not a 20 line file."
/etc/hosts is not a 20 line file.
[root@localhost tmp]# wc -l /etc/hosts| grep "^2" || echo "/etc/hosts is  a 2 line file."
2 /etc/hosts
[root@localhost tmp]# wc -l /etc/hosts| grep "^2" && echo "/etc/hosts is  a 2 line file."
2 /etc/hosts
/etc/hosts is  a 2 line file.
[root@localhost tmp]# 
  • case
    case提供了一种更加清晰,易于编写以及更具可读性的代替if/then/else的结构,尤其是在对多个值进行测试的情况下。在case语句中,我们列出要识别以及要操作的值,然后为每个值提供一个代码块。基本的case块如下所示:
[root@localhost tmp]# cat fruit.sh 
#!/bin/bash

read -p "What is your favorite fruit? : " fruit
case $fruit in
orange) echo "The $fruit is orange" ;;
banana) echo "The $fruit is yellow" ;;
pear)   echo "The $fruit is green"  ;;
*) echo "I don't know what color a $fruit is " ;;
esac 

[root@localhost tmp]#
[root@localhost tmp]# ./fruit.sh 
What is your favorite fruit? : banana    
The banana is yellow
[root@localhost tmp]# ./fruit.sh 
What is your favorite fruit? : apple
I don't know what color a apple is 
[root@localhost tmp]# 
# cat uname-case.sh 
#!/bin/bash
OS=`uname -s`
case "$OS" in
  FreeBSD) echo "This is FreeBSD" ;;
  CYGWIN_NT-5.1) echo "This is Cygwin" ;;
  SunOS) echo "This is Solaris" ;;
  Darwin) echo "This is Mac OSX" ;;
  AIX) echo "This is AIX" ;;
  Minix) echo "This is Minix" ;;
  Linux) echo "This is Linux" ;;
  *) echo "Failed to identify this OS" ;;
esac
[root@localhost]#
[root@localhost 5]# ./uname-case.sh 
This is Linux

使用通配符:

[root@localhost 5]# cat surname.sh 
#!/bin/bash

read -p "What is your surname?: " surname
case $surname in
  [a-g]* | [A-G]*) file=1 ;;
  [h-m]* | [H-M]*) file=2 ;;
  [n-s]* | [N-S]*) file=3 ;;
  [t-z]* | [T-Z]*) file=4 ;;
  *) file=0 ;;
esac
if [ "$file" -gt "0" ]; then
  echo "$surname goes in file $file"
else
  echo "I have nowhere to put $surname"
fi
[root@localhost 5]# 
[root@localhost 5]# ./surname.sh 
What is your surname?: Apple
Apple goes in file 1
[root@localhost 5]# ./surname.sh 
What is your surname?: apple
apple goes in file 1
[root@localhost 5]#
[root@localhost 5]# ./surname.sh 
What is your surname?: Parker
Parker goes in file 3
[root@localhost 5]# ./surname.sh 
What is your surname?: 'asdf
I have nowhere to put 'asdf
[root@localhost 5]#

如果设置了nocasematch,则没有必要重复大小写,因为大小写不区分。
关于case的bash实现的一个鲜为人知的特性是:可以使用;;&或;&而不是;;来终止语句,如果使用;&终止语句,则认为接下来的模式已经匹配。下例中,如果在语句末尾使用;;&表示需要继续执行测试。

[root@localhost 5]# cat case1.sh 
#!/bin/bash

read -p "Give me a word: " input
echo -en "You gave me some "
case $input in
  *[[:lower:]]*) echo -en "Lowercase " ;;&
  *[[:upper:]]*) echo -en "Uppercase " ;;&
  *[[:digit:]]*) echo -en "Numerical " ;;&
  *) echo "input." ;;
esac
[root@localhost 5]# 
[root@localhost 5]# ./case1.sh 
Give me a word: hello
You gave me some Lowercase input.
[root@localhost 5]# ./case1.sh 
Give me a word: Hello
You gave me some Lowercase Uppercase input.
[root@localhost 5]# ./case1.sh 
Give me a word: HELLO
You gave me some Uppercase input.
[root@localhost 5]# ./case1.sh 
Give me a word: 123
You gave me some Numerical input.
[root@localhost 5]# ./case1.sh 
Give me a word: Hello 123
You gave me some Lowercase Uppercase Numerical input.
[root@localhost 5]# ./case1.sh 
Give me a word: !@#
You gave me some input.
[root@localhost 5]#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值