Results 1 to 8 of 8

Thread: Multiple expression if statement - Shell script

  1. #1
    Join Date
    Sep 2013
    Beans
    2

    Multiple expression if statement - Shell script

    Dear All,
    I Have value for the below 6 variables.

    s1=${oparray['0']};
    s2=${oparray['1']};
    s3=${oparray['2']};
    s4=${oparray['3']};
    s5=${oparray['4']};
    s6=${oparray['5']};

    I want to check the below condition, but i am getting the error message like missing '] , binary operator expected, unexpected token `]' and [: too many arguments

    if [ [ "$s1" -eq "X" -and "$s2" -eq "$s1" -and "$s3" -eq "$s1" ] -or [ "$s4" -eq "X" -and "$s5" -eq "$s4" -and "$s6" -eq "$s4" ] ]
    then
    return 1
    else
    return 0
    fi

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Multiple expression if statement - Shell script

    Hi and welcome to the forums!

    Please check out BashFAQ 017 an d BashPitfalls 006 and 011 (links in my signature).

  3. #3
    Join Date
    Sep 2013
    Beans
    2

    Re: Multiple expression if statement - Shell script

    unable to find solution for this. kindly suggest

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Multiple expression if statement - Shell script

    What shell are you using? Are you trying to compare strings or integers?

    BTW, you can learn more about the if statement here: http://mywiki.wooledge.org/BashGuide..._and_.5B.5B.29

    EDIT:

    Thread moved to Programming Talk.

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Multiple expression if statement - Shell script

    methinks you misunderstand the meaning of brackets. They are not a weird flavor of infinitely stackable parentheses that can be used to create something like [[[[]][[]][]]].
    There is [ ] as one construct, and then there is [[ ]] which is kind of like an improved version of [ ] with more convenience features
    In other words [[ ... ] -or [ ... ]] mixes both and thus doesn't make sense = syntax error

    go with [ ... ] || [ ... ]
    or [[ ... ]] || [[ ... ]],
    or [ ... ] || [[ ... ]],
    or [[ ... ]] || [ ... ]
    Last edited by Vaphell; September 8th, 2013 at 10:18 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Multiple expression if statement - Shell script

    Also you need to use parentheses to combine expressions, not square brackets:
    Code:
    if ([ "$s1" -eq X ] && [ "$s2" -eq "$s1" ] && [ "$s3" -eq "$s1" ]) || \
       ([ "$s4" -eq Y ] && [ "$s5" -eq "$s4" ] && [ "$s6" -eq "$s1" ]); then
    	return 1
    else
    	return 0
    fi
    Last edited by prodigy_; September 8th, 2013 at 11:37 AM.

  7. #7
    Join Date
    May 2005
    Location
    Lyon, France
    Beans
    917
    Distro
    Ubuntu Development Release

    Re: Multiple expression if statement - Shell script

    wondering if the following is correct. (it works on my computer, doesn't mean it's well written)

    corrections made after reading next reply by prodigy...

    Code:
    #!/bin/bash
    
    # find out if the first 3 values, or the last 3 values, of array1
    # are equal to the value of input
    
    echo -n "Enter the array values: "; read array1
    array1=($array1)
    
    echo -n "Enter the value to compare: "; read input
    array2=($input $input $input)
    
    for i in {0..2}
    	do
    		array3+=(${array1[$i]})
    	done
    	
    if [[ ${array3[*]} == ${array2[*]} ]]; then
    	return 0
    else
    	unset array3
    	for i in {3..5}
    		do
    			array3+=(${array1[$i]})
    		done
    
    	if [[ ${array3[*]} == ${array2[*]} ]]; then
    		return 0
    	else
    		return 1
    	fi
    fi
    }
    
    testarray
    echo "returned value = $?"
    Last edited by bluefrog; September 8th, 2013 at 04:27 PM.
    James Dupin
    IT contractor
    Project delivery specialist
    http://fr.linkedin.com/in/jamesdupin

  8. #8
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Multiple expression if statement - Shell script

    If it works, it's correct but if you're going to use it in an actual script, using variable names like $a is a bad idea. And constructs like c[${#c[*]}] are simply unreadable.

    There's an old joke about Perl: You write some code that works and nobody but you can understand how it works. In 6 months you don't get it either. I'd say you achieved the same effect with bash. :)
    Last edited by prodigy_; September 8th, 2013 at 02:11 PM.

Bookmarks

Posting Permissions

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