Mat.S
Mat.S

Reputation: 1854

If file in Unix

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?

Upvotes: 0

Views: 131

Answers (2)

Marian
Marian

Reputation: 7472

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.

Upvotes: 0

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11603

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.

Upvotes: 1

Related Questions