【发布时间】:2013-12-14 23:05:13
【问题描述】:
我有一个涉及 shell 脚本并比较其中的值/变量的项目。我在此处和其他地方查看了比较变量,并尝试了给出的所有各种示例,但我遇到了一些不像宣传的那样。操作系统是Solaris10
我创建了以下脚本作为学习经验-
#!/bin/ksh
stest()
{
if $X = $Y
then echo they're the same
else echo they're notthe same
fi
}
X=a
Y=a
stest
echo completed
我不断得到以下的一些变化-
使用 shell sh 或 ksh-
#./test.sh
./test.sh[2]: a: not found
completed
使用 shell bash-
#./test.sh
./test.sh: line 5: a: command not found
completed
我尝试将if $X = $Y 行括在括号和双括号中,然后我回来了
[a: not found
或
[[a: not found
如果我将变量 X 和 Y 更改为数字“1”,我会得到同样的结果-
./test.sh[2]: 1: not found
我试过用单引号、双引号和反引号括起来。
感谢任何帮助。
【问题讨论】:
-
对于字符串,请尝试
if [ $X == $Y ]tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html -
那行得通 :) 谢谢!
标签: shell scripting sh ksh solaris-10