当前位置: 技术问答>linux和unix
关于Linux的shell简单编程的问题
来源: 互联网 发布时间:2016-07-11
本文导语: 这是一个简单的shell程序,我觉得已经没什么问题了,在linux下执行 # !/bin/bash echo "Please input 'Yes' or 'No' to display greetings:" read timeofday if ["$timeofday" = "Yes"] then echo "Good Morning!" elif ["$timeofday" = "No"]...
这是一个简单的shell程序,我觉得已经没什么问题了,在linux下执行
# !/bin/bash
echo "Please input 'Yes' or 'No' to display greetings:"
read timeofday
if ["$timeofday" = "Yes"] then
echo "Good Morning!"
elif ["$timeofday" = "No"] then
echo "Good afternoon!"
else
echo "Error Time!"
fi
使用chmod 给shell.sh添加执行权限
chmod +x shell.sh
最后,./shell.sh
会出现Please input 'Yes' or 'No' to display greetings:的提示:
输入"Yes"就会出现问题出现以下语句
command not found
syntax error near unexpected token 'then'
请高手指点一下,谢谢了。。。。
# !/bin/bash
echo "Please input 'Yes' or 'No' to display greetings:"
read timeofday
if ["$timeofday" = "Yes"] then
echo "Good Morning!"
elif ["$timeofday" = "No"] then
echo "Good afternoon!"
else
echo "Error Time!"
fi
使用chmod 给shell.sh添加执行权限
chmod +x shell.sh
最后,./shell.sh
会出现Please input 'Yes' or 'No' to display greetings:的提示:
输入"Yes"就会出现问题出现以下语句
command not found
syntax error near unexpected token 'then'
请高手指点一下,谢谢了。。。。
|
# !/bin/bash
echo "Please input 'Yes' or 'No' to display greetings:"
read timeofday
if [ "$timeofday" = "Yes" ]; then #[]左方括号的右边要有空格,右方括号的左边也要有空格
echo "Good Morning!"
elif [ "$timeofday" = "No" ]; then #并且then要和if放在一行,那么then前面必须要有分号这是两条语句
echo "Good afternoon!"
else
echo "Error Time!"
fi
|
有几个问题:
1、判断字符串相等用“==”符号
2、if和then间加“;”
1、判断字符串相等用“==”符号
2、if和then间加“;”
#!/bin/sh
echo "Please input 'Yes' or 'No' to display greetings:"
read timeofday
if [ "$timeofday" == "Yes" ] ; then
echo "Good Morning!"
elif [ "$timeofday" == "No" ] ; then
echo "Good afternoon!"
else
echo "Error Time!"
fi
|
貌似后面要用分号隔开,不隔开一行只能识别一句语句。
if ["$timeofday" = "Yes"]; then
echo "Good Morning!"
elif ["$timeofday" = "No"]; then
if ["$timeofday" = "Yes"]; then
echo "Good Morning!"
elif ["$timeofday" = "No"]; then
|
再之,使用=和==都是可以的使用=, 那么=号两边要有空格
使用==就不需要了
使用==就不需要了
|
左方括号的右边要有空格,右方括号的左边也要有空格
then和if放一行 前面要加;
楼主先学下shell编程基础哦
then和if放一行 前面要加;
楼主先学下shell编程基础哦
|
判断字符串相等用“==” 而且 if和then间加“;”