我有一个项目涉及使用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 ]
。参考链接:http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html - Jerry Jeremiah