LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3029|回复: 6

新手求帮助:一个简单shell的错误?!

[复制链接]
发表于 2005-7-31 14:20:44 | 显示全部楼层 |阅读模式
[root@localhost shell]#vi iftest2
#!/bin/bash
#iftest2
echo -n "enter your name:"
read NAME
#did the user just hit return:
if[ "$NAME" == "" ];
then
       echo "you did not enter any information"
else
       echo "your name is ${NAME}"
fi


[root@localhost shell]# ./iftest2
enter your name:
./iftest2: line 6: if[  ==  ]: command not found
./iftest2: line 7: syntax error near unexpected token `then'
./iftest2: line 7: ` then'




[root@localhost shell]# ./iftest2
enter your name:liuya
./iftest2: line 6: if[ liuya ==  ]: command not found
./iftest2: line 7: syntax error near unexpected token `then'
./iftest2: line 7: ` then'


请教,偶刚学shell,,,,,,先谢过了。
发表于 2005-7-31 14:27:05 | 显示全部楼层
Post by 鱼鱼游
[root@localhost shell]#vi iftest2
#!/bin/bash
#iftest2
echo -n "enter your name:"
read NAME
#did the user just hit return:
if[ "$NAME" == "" ];
then
       echo "you did not enter any information"
else
       echo "your name is ${NAME}"
fi


[root@localhost shell]# ./iftest2
enter your name:
./iftest2: line 6: if[  ==  ]: command not found
./iftest2: line 7: syntax error near unexpected token `then'
./iftest2: line 7: ` then'




[root@localhost shell]# ./iftest2
enter your name:liuya
./iftest2: line 6: if[ liuya ==  ]: command not found
./iftest2: line 7: syntax error near unexpected token `then'
./iftest2: line 7: ` then'


请教,偶刚学shell,,,,,,先谢过了。

if[
改成if空格[

编程需要有女人般的细心
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-31 14:35:09 | 显示全部楼层
Post by kiron
if[
改成if空格[

编程需要有女人般的细心



谢谢斑竹大哥,成功了。

这点还真忽略了。 :rolleyes:

我还有一点:

if[ "$NAME" == "" ];
then

分号,我要是把then放在第2行的话,还能不能加分号呢?

谢谢!
回复 支持 反对

使用道具 举报

发表于 2005-7-31 14:37:54 | 显示全部楼层
Post by 鱼鱼游
谢谢斑竹大哥,成功了。

这点还真忽略了。 :rolleyes:

我还有一点:

if[ "$NAME" == "" ];
then

分号,我要是把then放在第2行的话,还能不能加分号呢?

谢谢!


try,and get it.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-31 14:43:19 | 显示全部楼层
我看资料上说then要是跟在[]后面的话就是要[];
但我测试了下,跟不跟都可以正常显示结果,好象没有什么影响。不过以后还是要向标准看齐了,免的出现莫名其妙的错误。。。

谢谢斑竹。
回复 支持 反对

使用道具 举报

发表于 2005-7-31 14:48:25 | 显示全部楼层
Post by 鱼鱼游
我看资料上说then要是跟在[]后面的话就是要[];
但我测试了下,跟不跟都可以正常显示结果,好象没有什么影响。不过以后还是要向标准看齐了,免的出现莫名其妙的错误。。。

谢谢斑竹。

你是怎么测试的,在我的系统里:
if [....] then
和if [....]; then
是不一样的,后面的是对的.
从逻辑上想,也明白后面的才是对的.
你可以把你是怎么测试的说一下,让大家一起看看是怎么回事.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-8-2 14:07:39 | 显示全部楼层
Post by kiron
你是怎么测试的,在我的系统里:
if [....] then
和if [....]; then
是不一样的,后面的是对的.
从逻辑上想,也明白后面的才是对的.
你可以把你是怎么测试的说一下,让大家一起看看是怎么回事.



我是说这样:
---------------------
if [....];
then
---------------------
if [....]; then

---------------------
我的测试系统是redhatlinux9.


应该是;号在[]后面一直都应该跟上都不会出错.

可能是我前面没有说清楚,不好意思

斑竹真细心啊,,佩服..
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表