小编use*_*191的帖子

比较shell脚本中的变量

我有一个涉及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
Run Code Online (Sandbox Code Playgroud)

我不断得到以下的一些变化 -

使用shell sh或ksh-

#./test.sh
./test.sh[2]: a:  not found
completed
Run Code Online (Sandbox Code Playgroud)

使用shell bash-

#./test.sh
./test.sh: line 5: a: command not found
completed
Run Code Online (Sandbox Code Playgroud)

我已经尝试if $X = $Y用括号和双括号括起来,然后我回来了

[a:  not found  
Run Code Online (Sandbox Code Playgroud)

要么

[[a:  not found
Run Code Online (Sandbox Code Playgroud)

如果我将变量X和Y更改为数字"1",我会得到相同的东西 -

./test.sh[2]: 1:  not found
Run Code Online (Sandbox Code Playgroud)

我试过用单引号,双引号和反向引号括起来.

任何帮助表示赞赏.

shell scripting ksh sh solaris-10

8
推荐指数
2
解决办法
4万
查看次数

标签 统计

ksh ×1

scripting ×1

sh ×1

shell ×1

solaris-10 ×1