SHELL脚本之case

  • 一般形式
case variable in
    pattern1 | pattern2)
        command;;
    pattern3)
        command;;
    *)
        default command;;
esac
  • 例子
#!/bin/bash
case $USER in
    root | user)
        echo "Welcome.$USER"
        echo "Please enjoy your visit";;
    testing)
        echo "Special testing acount";;
    *)
    echo "Sorry,you are not allowed here";;
esac

SHELL脚本之if

if语句的几种常用方法

  • if then fi
if command
then
    commands
fi
  • if then else fi
if command
then
    commands
else
    commands
fi
  • if then elif then fi
if command1
then
    commands
elif command2
then
    more commands
fi

PS:
1.if-then语句允许你使用布尔逻辑来组合测试,有两种布尔运算符可用:
[ condition ] && [ condition2 ]
[ condition ] || [ condition2 ]
2.双圆括号命令允许你将高级数学表达式放入比较中:
(( expression ))
3.双方括号里的expression使用了test命令中采用的标准字符串进行比较。但他提供了test命令未提供的另一种特性—-模式匹配
[[ expression ]]