If file in Unix

帅比萌擦擦* 提交于 2019-12-13 04:02:45

问题


I have a code

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

that I have typed and the return statement is

a*

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

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?


回答1:


Spacing issue, you need to do

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

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"* ]]

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




回答2:


You probably wanted something like:

if [ "a1 a2" = "`echo a*`" ]
...

which is checking whether there are exactly two files (a1 and a2) in the working directory starting by the prefix a.



来源:https://stackoverflow.com/questions/21505940/if-file-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!