开发者

shell script (ksh) + compare sign as param is it possible?

开发者 https://www.devze.com 2023-01-08 22:31 出处:网络
Subject shell script if I perform compare on the following [[ 2 -eq 2 ]] && print OK I get OK

Subject shell script

if I perform compare on the following

 [[ 2 -eq 2 ]] && print OK

I get OK

But how to compare if the "-eq" is in my param

for example

param="-eq"

  [[ 2 $p开发者_JS百科aram 2 ]] && print OK

Obvious that not illegal

but I wonder if it possible anyway with some changes?? Lidia


The [[ construct was designed precisely so that operators (such as -eq) must be specified directly, to avoid strings coming from parameter expansion being accidentally interpreted as parameters.

Use [, which is an ordinary command with fairly similar syntax to [[. Since it's an ordinary command, parameter expansion happens normally.

[ 2 $param 2 ] && print OK


I'm not sure why you would want to do this, but you could use eval for this:

eval [[ 2 $param 2 ]] && print OK
0

精彩评论

暂无评论...
验证码 换一张
取 消