views:

81

answers:

5

Hi,

I'm very new to shell scripting and i've been struggling with the following shell script. I'm posting the script and the commands i used below for your consideration please help me with the mistake i made.

#
#
#

DBG=0
RLS=0
ALL=0
CLN=0

print_help_uu()
{
        echo "Usage: $0 -D -R -A -C "; 
        echo "Where -C clean the debug project builds";
        echo "      -D to build in DEBUG config";
        echo "      -R to build in RELEASE config";
        echo "      -A to build in both configs";
        return
}

#
# Main procedure start here
#
# Check for sufficent args
#

if [ $# -eq 0 ] ; then
        print_help_uu
        exit 1
fi    

#
# Function to clean the project
#
clean()
{
        if ["$DBG"="1"]; then
            echo "Cleaning debug"

            if ["$RLS"="1"]; then
                echo "cleaning release + debug"
            else
                echo "This is bad"
            fi
        fi

        if ["$RLS"="1"]; then 
            echo "Cleaning release "
        fi
        return
}


while getopts "DRAC" opt
do
        case "$opt" in
                D) DBG=1;;
                R) RLS=1;;
                A) DBG=1;RLS=1;;
                C) CLN=1;;
                \?) print_help_uu; exit 1;; 
        esac
        clean
done   

I'm posting the commands i used to run it and the errors i got when using those commands.

----------
./BuildProject.sh -D
./BuildProject.sh: line 36: [1=1]: command not found
./BuildProject.sh: line 46: [0=1]: command not found

-----------
sh BuildProject.sh -D
BuildProject.sh: 63: [1=1]: not found
BuildProject.sh: 63: [0=1]: not found

-----------
sh ./BuildProject.sh -D
./BuildProject.sh: 63: [1=1]: not found
./BuildProject.sh: 63: [0=1]: not found

I tried to solve it in soo many ways and googled a lot before posting here. But all my trials went in vain. Please tell me where i'm doing the mistake since i'm new to shell scripting.

Thanks in Advance.

+3  A: 

[ is a command, but you are trying to invoke the command [1=1]. Add some whitespace:

if [ "$DBG" = "1" ]; then
Marcelo Cantos
+2  A: 

i think it's a "SPACE" problem : try

if [ "$DBG" = "1" ]; then

instead of

if ["$DBG"="1"]; then
chburd
+4  A: 

Try to change ["$DBG"="1"] (and similar if statements) into this: [ "$DBG" = "1" ] i.e. add some space.

Emil Vikström
A: 

It Worked after adding some extra spaces into it. Thank you all. Is it a Scripting rule to put those spaces in between the variables?? I think i ignored that rule. Thanks for your time.

Jabez
It's a syntax rule in Bash. It is not needed for all scripting/programming languages, but for some it does and Bash is one such language.
Emil Vikström
Thank you Emil for enlightening me.
Jabez
+1  A: 

It's a space issue indeed.

VAR=VALUE

is only for variable declaration in shell, while

VAR = VALUE

is only for variable testing. It's tricky, you just have to get used to it.

Raphink
Got the Trick Raphink. Thank you.
Jabez