4

I have a bash statement to test a command line argument. If the argument passed to the script is "clean", then the script removes all .o files. Otherwise, it builds a program. However, not matter what is passed (if anything), the script still thinks that the argument "clean" is being passed.

#!/bin/bash
if test "`whoami`" != "root" ; then
    echo "You must be logged in as root to build (for loopback mounting)"
    echo "Enter 'su' or 'sudo bash' to switch to root"
    exit
fi
ARG=$1
if [ $ARG == "clean" ] ; then
    echo ">>> cleaning up object files..."
    rm -r src/*.o
    echo ">>> done. "
    echo ">>> Press enter to continue..."
    read
else
    #Builds program
fi

3 Answers 3

9

Answer for first version of question

In bash, spaces are important. Replace:

[ $ARG=="clean" ]

With:

[ "$ARG" = "clean" ]

bash interprets $ARG=="clean" as a single-string. If a single-string is placed in a test statement, test returns false if the string is empty and true if it is non-empty. $ARG=="clean" will never be empty. Thus [ $ARG=="clean" ] will always return true.

Second, $ARG should be quoted. Otherwise, if it is empty, then the statement reduces to `[ == "clean" ] which is an error ("unary operator expected").

Third, it is best practices to use lower or mixed case for your local variables. The system uses upper-case shell variables and you don't want to accidentally overwrite one of them.

Lastly, with [...], the POSIX operator for equal, in the string sense, is =. Bash will accept either = or == but = is more portable.

Sign up to request clarification or add additional context in comments.

7 Comments

Edited the main post, but I still have the same issue
@Kookerus Hmm. I can't reproduce that. With the addition of the spaces, the script works for me.
@Kookerus: Did you recognize the single '=' ?
@Manuel both = and == are pretty much equivalent. Changing to a single = doesn't fix the problem.
@John1024 removing the .sh extension and running sudo ./build gives the error ./build: line 10: ==: command not found
|
0

first: Every string must double quoted or will error absent argument.

second: for string used only = or != not a == and also -n and -z commands.

third: you may combine conditions by -a and -o commands but newer used enclose in () yous conditions so not to get error. Logical operators acts through operators presidence, fist calculate -o operator and then -a! For example

[ -n "$1" -a $1 = '-h' -o $1 = '--help' ] && { usage; exit 0; }

will work when passed to script at least 1 argument and is -h or --help. All spaces must be!!! Bush do short cycle logical evaluations. So don't trouble for case when $1 don't exist in second condition because of result of this expression is determined in first one. next don't calculate in this case. But if your argument may contains space symbols you need it double quote. You must do it also in command line too! Else you get error in script or split your arguments in two or more parts.

Comments

-1

Operator == isn't used in test. For numbers(not siring) used -eq or -ne commands. See man 1 test for full descriptions. test EXPRESSION... equivalent of [ EXPRESSIONS... ]. More shirt form of test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.