Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command not found when comparing string

Tags:

shell

I have this script

#!/bin/sh        
if [ $# -ne 3 ] ; then
            echo "param 1, param2, and A or B "
            exit 1;

    elif [ $# -eq 3 ]; then
                    if [$3 = "A"] ; then
                                    echo "A"
                    elif [$3 = "B"]; then
                                    echo "B"
                    fi

fi

It is basically checking if param 3 is A or B, and do echo. But it returns:

./test.sh: line 6: [A: command not found
./test.sh: line 8: [A: command not found

I tried to use -eq for comparison, but it still does not work. How can I fix this problem?

like image 381
sateayam Avatar asked Feb 15 '12 14:02

sateayam


People also ask

Why is command not found?

When you're trying to run a command (with or without sudo ) and get an error message that reads "Command not found," this means the script or file you're trying to execute doesn't exist in the location specified by your PATH variable.

Why Bash command not found?

If the specified package or command is not installed on your system, then you will get the output shown in the image below. Through this output, you can verify that the said package or command is not installed on your system, and because of this, you might be receiving the Bash Command Not Found error.

What is the difference between sh and Bash?

bash is sh, but with more features and better syntax. Bash is “Bourne Again SHell”, and is an improvement of the sh (original Bourne shell). Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems.

How do you check if a variable is equal to a string in Bash?

You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!= ” is used to check inequality of the strings. You can partially compare the values of two strings also in bash.


1 Answers

I'm sure this has come up before, but -- [ is actually a command. You need a space after the [ in order for the shell to find it.

like image 177
FatalError Avatar answered Dec 14 '22 23:12

FatalError