Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing command-line parameter to a string

Tags:

bash

unix

Here is my code:

#!/bin/bash if [ "$#" -ne 2 ] ; then         echo "$0: exactly 2 arguments expected"         exit 3 fi  if [$1 != "file" -a $1 != 'dir'] ; then         echo "$0: first argument must be string "file" or "dir""         exit 1 elif [-e $2 -a -r $2]; then         if ["$1" = "file" -a -f $2] ; then                 echo YES         elif ["$1" = "dir" -a -d $2] ; then                 echo YES         else                 echo NO         fi         exit 0 else         echo "$0: $2 is not a readable entry"         exit 2 fi 

If I run ./lab4 file filename1 it will check if the first parameter is the string "file" or "dir" then if the first parameter is "file" and filename1 is a file, it will print yes. Same thing for dir.

It doesn't recognize $1 and $2. The code will output:

./lab04Q2: line 7: [file: command not found ./lab04Q2: line 10: [-e: command not found 

even though I did put 2 parameters when running the program.

like image 296
JA3N Avatar asked Feb 13 '12 02:02

JA3N


People also ask

Which of the following commands would you use to check if two strings are equal?

Use == operator with bash if statement to check if two strings are equal. You can also use != to check if two string are not equal. You must use single space before and after the == and !=

How do you compare 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.

How do you compare arguments in Java?

Compare Strings Using the Equals() Method equals() method is the Object class method, String class overrides it. equals() method compare two strings for value equality, whether they are logically equal. equals() method in String class takes another string as a parameter and compares it with the specified string.


2 Answers

Try the following 3 lines in bash:

if [ "a" == "a" ]; then echo hi; fi if ["a" == "a" ]; then echo hi; fi if ["a" == "a"]; then echo hi; fi 

You'll see that only the first one works, whereas the other two do not. i.e. it's your lack of spaces is the reason why your expression doesn't work.

The above example also suggests that you can test out bash syntax directly on the bash prompt. You can get it right before incorporating them into your script.

like image 158
Stephen Quan Avatar answered Sep 20 '22 15:09

Stephen Quan


The problem(s) come from the fact that [ is actually a command. In fact it's an alias for the test command. In order for this to run properly, you'll need to add a space after your [ as in:

if [ $1 != "file" -a $1 != 'dir' ] ; 

Do this for all your instances of [ that don't have a space after it.

P.S.

Since you're using bash as your interpreter, I highly suggest you use [[ ]] instead of [ ] for your tests as the former is a lot more capable than the latter with no downsides; no need for a space is one of them

like image 35
SiegeX Avatar answered Sep 20 '22 15:09

SiegeX