高手看过来
-
- 帖子: 10
- 注册时间: 2006-10-25 20:42
高手看过来
#!/bin/bash
if [ $1="yes" ] ; then
clear
echo $1
echo "Telephone Directory"
echo " "
echo "1.Find Listing:"
echo "2.Add Listing"
echo "3.Modify Listing"
echo "4.Print Listing"
echo "Q.quit"
echo " "
read selection
case $selection in
"1")
findlisting
;;
"2")
addlisting
;;
"3")
modifylisting
;;
"4")
printlisting
;;
"Q")
exit 0
esac
else
echo "wrong choice"
fi
****************************************************
为什么,第二行参数判断不起作用?
不管有没有参数,shell都能运行?
if [ $1="yes" ] ; then
clear
echo $1
echo "Telephone Directory"
echo " "
echo "1.Find Listing:"
echo "2.Add Listing"
echo "3.Modify Listing"
echo "4.Print Listing"
echo "Q.quit"
echo " "
read selection
case $selection in
"1")
findlisting
;;
"2")
addlisting
;;
"3")
modifylisting
;;
"4")
printlisting
;;
"Q")
exit 0
esac
else
echo "wrong choice"
fi
****************************************************
为什么,第二行参数判断不起作用?
不管有没有参数,shell都能运行?
-
- 帖子: 1331
- 注册时间: 2006-07-09 12:22
- 来自: 南京
-
- 帖子: 10
- 注册时间: 2006-10-25 20:42
-
- 帖子: 26
- 注册时间: 2006-02-07 15:05
- 5451vs5451
- 帖子: 345
- 注册时间: 2006-07-14 18:56
- 来自: Apple Valley, Planet Tux, Linux System
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
- 5451vs5451
- 帖子: 345
- 注册时间: 2006-07-14 18:56
- 来自: Apple Valley, Planet Tux, Linux System
看到过这种写法,但一直不明白为什么要加那个x。这样写也不会报错啊:wkt 写了:if [ $1 == "yes" ] 建议这样写 if [ "x$1" == "xyes" ]
没有参数就不会报错了
检验是否有参数可以
if [ "x$1" == "x" ];then
echo "no arguements"
exit 1
else
echo "haha $@"
fi
代码: 全选
if [ "$1" == "yes" ]
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
用bash 不会5451vs5451 写了:看到过这种写法,但一直不明白为什么要加那个x。这样写也不会报错啊:wkt 写了:if [ $1 == "yes" ] 建议这样写 if [ "x$1" == "xyes" ]
没有参数就不会报错了
检验是否有参数可以
if [ "x$1" == "x" ];then
echo "no arguements"
exit 1
else
echo "haha $@"
fi代码: 全选
if [ "$1" == "yes" ]
但是sh会
代码: 全选
wkt@ubuntu:~
-0-$ cat /tmp/bash
if [ "$1" == "yes" ];then
echo "hhh"
fi
wkt@ubuntu:~
-0-$ sh /tmp/bash
[: 3: ==: unexpected operator
wkt@ubuntu:~
-0-$ bash /tmp/bash
wkt@ubuntu:~
-0-$
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
用bash 不会5451vs5451 写了:看到过这种写法,但一直不明白为什么要加那个x。这样写也不会报错啊:wkt 写了:if [ $1 == "yes" ] 建议这样写 if [ "x$1" == "xyes" ]
没有参数就不会报错了
检验是否有参数可以
if [ "x$1" == "x" ];then
echo "no arguements"
exit 1
else
echo "haha $@"
fi代码: 全选
if [ "$1" == "yes" ]
但是sh会
代码: 全选
wkt@ubuntu:~
-0-$ cat /tmp/bash
if [ "$1" == "yes" ];then
echo "hhh"
fi
wkt@ubuntu:~
-0-$ sh /tmp/bash
[: 3: ==: unexpected operator
wkt@ubuntu:~
-0-$ bash /tmp/bash
wkt@ubuntu:~
-0-$
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
用bash 不会5451vs5451 写了:看到过这种写法,但一直不明白为什么要加那个x。这样写也不会报错啊:wkt 写了:if [ $1 == "yes" ] 建议这样写 if [ "x$1" == "xyes" ]
没有参数就不会报错了
检验是否有参数可以
if [ "x$1" == "x" ];then
echo "no arguements"
exit 1
else
echo "haha $@"
fi代码: 全选
if [ "$1" == "yes" ]
但是sh会
代码: 全选
wkt@ubuntu:~
-0-$ cat /tmp/bash
if [ "$1" == "yes" ];then
echo "hhh"
fi
wkt@ubuntu:~
-0-$ sh /tmp/bash
[: 3: ==: unexpected operator
wkt@ubuntu:~
-0-$ bash /tmp/bash
wkt@ubuntu:~
-0-$
- 5451vs5451
- 帖子: 345
- 注册时间: 2006-07-14 18:56
- 来自: Apple Valley, Planet Tux, Linux System
这么写是不对的,请看下面的例子:Robinho 写了:也可以使用双圆括号结构
if (( $1 == "yes" ))
代码: 全选
$ a=no; if (( a == "no" )); then echo yes; else echo no; fi
yes
$ a=no; if (( a == "yes" )); then echo yes; else echo no; fi
yes
$ a=no; if (( a == 0 )); then echo yes; else echo no; fi
yes
$ a=no; if (( a == 1 )); then echo yes; else echo no; fi
no