快速了解shell脚本(2)
这次了解shell的一些比较高级的用法。
字符串
字符串可以用单引号,也可以用双引号,也可以不用引号,单引号里的任何字符都会原样输出,跟PHP中的单双引号类似。
条件判断 && 流程控制
和JavaScript等语言不一样,shell的流程控制不可为空,如果else分支没有语句执行,就不要写else。
if else语句
// if
if condition
then
command1
command2
...
commandN
fi
// 写成一行(适用于终端命令提示符):
// 末尾的fi就是if倒过来拼写
if `ps -ef | grep ssh`; then echo hello; fi
// if else
if condition
then
command1
command2
...
commandN
else
command
fi
// if else-if else
if condition1
then
command1
elif condition2
command2
else
commandN
fi
for while
// for
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
// 写成一行:
for var in item1 item2 ... itemN; do command1; command2… done;
// while
while condition
do
command
done
// case
case "${opt}" in
"Install-Puppet-Server" )
install_master $1
exit
;;
"Install-Puppet-Client" )
install_client $1
exit
;;
"Config-Puppet-Client" )
config_puppet_client
exit
;;
"Exit" )
exit
;;
* ) echo "Bad option, please choose again"
// case的语法PHP、java这类语言差别很大,它需要一个esac(就是case反过来)作为结束标记,每个case分支用右圆括号,用两个分号表示break
这个人暂时没有 freestyle