Shell基础脚本-嵌套 if/then 条件测试
2021-11-18 本文已影响0人
Chris0Yang
可以嵌套 if/then 条件测试结构
嵌套的结果等价于使用 && 复合比较运算符
a=3
if [ "$a" -gt 0 ]
then
if [ "$a" -lt 5 ]
then
echo "The value of \"a\" lies somewhere between 0 and 5."
fi
fi
# 和下面的结果相同
if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
then
echo "The value of \"a\" lies somewhere between 0 and 5."
fi
测试题目
系统文件 xinitrc 可以用来启动软件 X Server
该文件包含了许多 if/then 测试结构
下面的代码摘录自较早版本的 xinitrc(大约在 Red Hat 7.1 版本)
if [ -f $HOME/.Xclients ]; then
exec $HOME/.Xclients
elif [ -f /etc/X11/xinit/Xclients ]; then
exec /etc/X11/xinit/Xclients
else
# 安全分支。尽管程序不会执行这个分支。
# (我们在 Xclients 中也提供了相同的机制)增强程序可靠性。
xclock -geometry 100x100-5+5 &
xterm -geometry 80x50-50+150 &
if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then
netscape /usr/share/doc/HTML/index.html
fi
fi
试着解释代码片段中的条件测试结构, 然后试着在 /etc/X11/xinit/xinitrc 查看最新版本,并且分析其中的 if/then 条件测试结构