Find the answer to your Linux question:
Results 1 to 9 of 9
Code: #!/bin/bash echo $1 if [ $1=="A" ]; then echo 0 > Type.txt else if [ $1=="B" ]; then echo 1 > Type.txt else if [ $1=="C" ]; then echo ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5

    My code seems to think the first if statement is always the truth!!

    Code:
    #!/bin/bash
    
    
    echo $1
    
    if [ $1=="A" ];
    	
    	then echo 0 > Type.txt
    else
    	
    	if [ $1=="B" ];
    		then echo 1 > Type.txt
    	else
    		if [ $1=="C" ];
    			then echo 2 > Type.txt
    		else
    			if [ $1=="D" ];
    				
    				then echo 3 > Type.txt
    					
    			else
    				echo "Fail"
    			fi
    		fi
    	fi
    fi
    My code seems to always think the first if statement is true. What is wrong?

  2. #2
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    Put a space between your "==" operator and its operands.

    Lets say, for example, $1 is the empty string.
    If you write if [ $1=="A" ]; without spaces between $1 and == and "A",
    this is the same as writing if [ "$1==A" ];
    and the string will evaluate to "==A",
    which is non-null and therefore is always true.

    What you want is this:
    Code:
    if [ "$1" == "A" ]:
    then ...
    else ...
    fi
    Although this code would be better:
    Code:
    if   [ "$1" == "A" ]
    then echo 0 >Type.txt
    elif [ "$1" == "B" ]
    then echo 1 >Type.txt
    elif [ "$1" == "C" ]
    then echo 2 >Type.txt
    elif [ "$1" == "D" ]
    then echo 3 >Type.txt
    else echo "Fail"
    fi
    And this code would be best:
    Code:
    case "$1" in
      ("A") echo 0 >Type.txt ;;
      ("B") echo 1 >Type.txt ;;
      ("C") echo 2 >Type.txt ;;
      ("D") echo 3 >Type.txt ;;
      (*) echo "Fail" ;;
    esac

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    31
    FWIW, even if your test worked correctly, you'd see the same result.

    if [ $1=="A" ];

    The semicolon terminates the if statement, so what the compiler would see is:

    if [ $1=="A" ] { };
    echo 0 > Type.txt

    i.e. two separate statements having nothing to do with each other other than position in the code.

  4. #4
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    Quote Originally Posted by Toadbrooks View Post
    FWIW, even if your test worked correctly, you'd see the same result.

    if [ $1=="A" ];

    The semicolon terminates the if statement, so what the compiler would see is:

    if [ $1=="A" ] { };
    echo 0 > Type.txt

    i.e. two separate statements having nothing to do with each other other than position in the code.
    I'm afraid you are incorrect.

    Try it for yourself on the command line, enter an if statement terminated with a colon, the interpreter will expect you to enter a "then" statement, and continue prompting you until you enter a "fi" statement:
    Code:
    % a="A"
    % if [ "$a" == "A" ]; then echo YES; else echo NO; fi
    YES
    % if [ "$a" == "B" ];
    ... then echo YES;
    ... else echo NO;
    ... fi;
    NO
    Yes, semi-colons are necessary after the condition of the "if" statement if the "then" statement is on the same line. However. if there is a semi-colon before a newline, it is simply ignored, it does NOT terminate the "if" statement. In fact, not following an "if" statement with a "then" statement and then a terminating "fi" statement will cause a syntax error regardless of your use of semi-colons.

    Its a good idea to test your explanation before posting it here. I've made that mistake myself, and posted wrong information. Now I always double check.

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    31
    Just retested it and it works that way for me. (Actually I get an "unmatched if" statement, presumably the dangling then, when I execute his original example.)

    Different version of bash? Who knows? I liked your case example. I do feel that programming according to the standard is the correct way to do things, and avoids situations like we have where it works for one and doesn't for the other.

  6. #6
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    Quote Originally Posted by Toadbrooks View Post
    Just retested it and it works that way for me. Different version of bash? Who knows?
    Hmm, mine won't let me terminate an if statement with a simicolon, even when following it with a "fi" statement.

    Yeah, maybe its a shell option or something that you've set that I haven't had set on mine. Who knows.

  7. #7
    Just Joined!
    Join Date
    Jul 2007
    Location
    Chennai, India
    Posts
    8
    I think bash only supports elif.

    So the logic should be:
    #!/bin/bash


    echo $1

    if [ $1=="A" ];
    then echo 0 > Type.txt
    elif [ $1=="B" ];
    then echo 1 > Type.txt
    elif [ $1=="C" ];
    then echo 2 > Type.txt
    elif [ $1=="D" ];
    then echo 3 > Type.txt
    else
    echo "Fail"
    fi

  8. #8
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    Quote Originally Posted by ananthap View Post
    I think bash only supports elif.
    No, you can have nested if statements, although using "elif" would be much better, and using "case" would be better still (like I said in my first response).

    Also you made the same mistake as the poster of this thread, you did NOT put a space between the "==" operator:
    Code:
    #Incorrect:
    if [ "$1"=="A" ]; then echo YES; else echo NO; fi
    #Correct:
    if [ "$1" == "A" ]; then echo YES; else echo NO; fi
    Without a space, the "==" operator is considered to be part of the string "A" (so bash sees "==A"), and because the string in the conditional is non-null, the conditional always evaluates to true.

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    It's also good practice to always put your positional parameters in quotes to avoid syntax errors if someone enters an argument with embedded spaces.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...