我是靠谱客的博主 威武巨人,这篇文章主要介绍Shell脚本之case语句,现在分享给大家,希望可以做个参考。

  • 没有对比,就没有伤害,先来看一下使用else if有多麻烦
[root@foundation8 test]# cat if_complex.sh 
#!/bin/bash
#looking for a possible value
if [ $USER = "student" ];then
echo "Welcome $USER"
echo "Please enjoy your visiting!"
elif [ $USER = "Bob" ];then
echo "Welcome $USER"
echo "Please enjoy your visiting!"
elif [ $USER = "Alice" ];then
echo "Welcome $USER"
echo "Please enjoy your visiting!"
elif [ $USER = "Coco" ];then
echo "Welcome $USER"
echo "Please enjoy your visiting!"
else
echo "Sorry,you're not allowed here."
fi
[root@foundation8 test]# sh if_complex.sh 
Sorry,you're not allowed here.
  • case闪亮登场
[root@foundation8 test]# cat case_better.sh 
#!/bin/bash
#using the case command
case $USER in
student|Bob|Alice|Coco)
echo "Welcome $USER"
echo "Please enjoy your visiting!"
;;
*)
echo "Sorry,you're not allowed here."
esac
[root@foundation8 test]# sh case_better.sh 
Sorry,you're not allowed here.

最后

以上就是威武巨人最近收集整理的关于Shell脚本之case语句的全部内容,更多相关Shell脚本之case语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部