如果文件在Unix中

如果文件在Unix中

问题描述:

我有一个密码

if ["a1 a2" = "a*"]
then
   echo match
else
   echo "a*"
fi

我输入的

并且return语句是

that I have typed and the return statement is

a *

main.ksh [3]:[a1 a2:找不到[没有这样的文件或目录]

main.ksh[3]: [a1 a2: not found [No such file or directory]

我想知道为什么会这样,我认为if语句只比较了字符串.它与文件和目录有什么关系?

I am wondering why this is, I thought the if statement only compared the strings. What does it have to do with files and directories?

间距问题,您需要做

if [ "a1 a2" = "a*" ]

否则,至少"a1将被视为测试运算符的一部分.

otherwise at least "a1 will get treated as part of the test operator.

还要进行正则表达式匹配,您需要做类似的事情

Also to do regex matching, you need to do something like

if [[ "a1 a2" =~ "a"* ]]

但是请注意,它将匹配a,然后在字符串中的任意位置匹配0个或多个字符,这可能不是您想要的.

But note that will match a followed by 0 or more characters anywhere in the string, which probably isn't what you want.