当前位置: 技术问答>linux和unix
新手shell的一个问题
来源: 互联网 发布时间:2016-05-17
本文导语: 小弟按照网上的例子输入如下程序: #!/bin/sh echo -n please input your id: read id_var echo -n please input your password: read password echo userid=$id_var echo password=$password if [$password='admin']; then echo "password is right" else echo "passwo...
小弟按照网上的例子输入如下程序:
#!/bin/sh
echo -n please input your id:
read id_var
echo -n please input your password:
read password
echo userid=$id_var
echo password=$password
if [$password='admin'];
then
echo "password is right"
else
echo "password is wrong"
fi
执行:sh f.sh ,出现错误提示:
line 8:[admin=admin]:commond not found
运动环境是REDFLAG 6.0
实在弄不清是哪儿有问题,有没有高手指点一下,谢谢!
#!/bin/sh
echo -n please input your id:
read id_var
echo -n please input your password:
read password
echo userid=$id_var
echo password=$password
if [$password='admin'];
then
echo "password is right"
else
echo "password is wrong"
fi
执行:sh f.sh ,出现错误提示:
line 8:[admin=admin]:commond not found
运动环境是REDFLAG 6.0
实在弄不清是哪儿有问题,有没有高手指点一下,谢谢!
|
没有此命令,关注。
|
楼主用这个代码运行,看看怎样:
[code=BatchFile]#chmod +x f.sh
#./f.sh[/code]
|
=号两边要有空格
|
方括号内侧也要空格
|
改成这个:
if [ "$password" = "admin" ]
if [ "$password" = "admin" ]
|
用if判断时[]前后都要有空格。
|
楼上几层正解,特别注意空格的存在。
|
同学习
试了一下,[]内侧加空格就可以了
试了一下,[]内侧加空格就可以了
|
3楼4楼给出了答案:
#!/bin/sh
echo -n please input your id:
read id_var
echo -n please input your password:
read password
echo userid=$id_var
echo password=$password
if [ $password = 'admin' ];
then
echo "password is right"
else
echo "password is wrong"
fi
#!/bin/sh
echo -n please input your id:
read id_var
echo -n please input your password:
read password
echo userid=$id_var
echo password=$password
if [ $password = 'admin' ];
then
echo "password is right"
else
echo "password is wrong"
fi
|
学习了