Chinaunix

标题: if [];then if的问题 [打印本页]

作者: wwh1234    时间: 2006-07-31 13:21
标题: if [];then if的问题
本人安装的系统是reht as版本
在test用户下vi创建了一个test1.sh文件
文件内容是:
read variable
if ["variable"=y];  then
  echo "good!"
else
  echo"bad!"
fi


运行sh  test1.sh
报错信息:command not found
请问各位高手这是什么原因阿
作者: xoyun    时间: 2006-07-31 13:24
read va
if [ "$va" = "y" ]; then
echo "good"
else
echo "bad"
fi
作者: wwh1234    时间: 2006-07-31 13:38
谢谢!!
read variable
if ["$variable"="y"];  then
  echo "good!"
else
  echo"bad!"
fi

我试过了,还是command not found
是不是PATH设得不好的问题啊?
作者: waker    时间: 2006-07-31 13:45
新手导航第七帖或读2楼回帖50遍
作者: wwh1234    时间: 2006-07-31 14:58
谢谢斑竹
但是我的问题这个上面没有提到
read variable
if ["$variable"="y"];  then
  echo "good!"
else
  echo"bad!"
fi

运行这个脚本时报错信息是
test.sh: line 2:[y=y] :command not found
test.sh: line 4:bad! :command not found
作者: xiaoyi1982    时间: 2006-07-31 15:02
比较一下,["$variable"="y"];   
和  [ "$variable" = "y" ];有什么区别。
作者: wwh1234    时间: 2006-07-31 15:08
有区别的
错误信息是
test.sh: line 2:[y  :command not found
test.sh: line 4:bad! :command not found
作者: xiaoyi1982    时间: 2006-07-31 15:13
空格
作者: ccf    时间: 2006-07-31 15:15
  1. if [ "$variable" = "y" ];  then
  2.   echo "good!"
  3. else
  4.   echo "bad!"
  5. fi
复制代码


空格的问题,不过我也解释不了为什么,我先去读版主的50遍去再说
作者: wwh1234    时间: 2006-07-31 15:15
对啊,等号2边都加上空格了,就出现了上面的错误信息。就是还是通不过
作者: ccf    时间: 2006-07-31 15:21
[]这个两边也要加,还有你的echo"bad"没空格
作者: xiaoyi1982    时间: 2006-07-31 15:24
括号两边也要有空格

或者你改为test "$variable" = "y"试一下.
作者: webyuhang    时间: 2006-07-31 15:24
读50遍。肯定可以记住。哈哈

[root@rd src]# read variable;if [ "$variable" = "y" ];then echo "good" ; else echo "flase"; fi
y
good
可以的。注意等号2边的空格

[root@rd src]# test "$variable" = "y"
[root@rd src]# echo $?
0
[root@rd src]# test "$variable" = "n"
[root@rd src]# echo $?
1
[root@rd src]#
作者: wwh1234    时间: 2006-07-31 15:27
谢谢各位,谢谢了。
运行成功了,许多书上都部描写这个细节的!!!
作者: xoyun    时间: 2006-07-31 16:01
原帖由 wwh1234 于 2006-7-31 14:58 发表
谢谢斑竹
但是我的问题这个上面没有提到
read variable
if ["$variable"="y"];  then
  echo "good!"
else
  echo"bad!"
fi

运行这个脚本时报错信息是
test. ...


这个是Shell程序规定的,没有什么原因,没按规范写肯定报错,你还没问脚本为什么要这样写呢?
作者: wwh1234    时间: 2006-07-31 16:22
我是初学者,在做练习的时候碰到了这个问题。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2