How to Use Multiple if Conditions in Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Use Multiple if Conditions in Shell script
# 1  
Old 06-25-2009
Question How to Use Multiple if Conditions in Shell script

if [[ "$val" -ge "36000" -a "$val" -le "42000" ] -o [ "$val" -ge "60000" -a "$val" -le "66000" ]]
then
echo "Expected valid value"


The above multiple if condition is NOT working in my script.
I am getting the error as '-a' not expected. Can anyone help with the syntax for this?
# 2  
Old 06-25-2009
depending on your shell, you may need to write like this...
Code:
if [[ ( "$val" -ge "36000" ) && ( "$val" -le "42000" ) ]] || [[ ( "$val" -ge "60000" ) && ( "$val" -le "66000" ) ]]

if it does not work, let me know your shell.
# 3  
Old 06-25-2009
Thank you sir..It is working..
Can you please tell me what was the problem with my script? is it depends on the shell?
# 4  
Old 06-25-2009
Yes. it depends on shell. In bash you use -a -o not in ksh.
and I have formatted your if command to make it more readable.
# 5  
Old 06-25-2009
Thanks for the quick reply
# 6  
Old 06-25-2009
No need for the use of '[[ ]]" twice within the if statement. It can be done with a single test.
Code:
if [[ ( "$val" -ge "36000"  &&  "$val" -le "42000" ) || ( "$val" -ge "60000"  &&  "$val" -le "66000" ) ]]

It can also be done more easily and cleanly using (( )) which works for both ksh and bash
Code:
if (( ( val > 36000  &&  val < 42000 ) || ( val > 60000  &&  val < 66000 ) ))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple If conditions

I am analyzing one of the scripts written by another person.script is having multiple if conditions and everything are nested.The code is not formatted properly.Is there any way to identify in Unix to identify begin and end of a particular if block? (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

2. Shell Programming and Scripting

How to use SQL Hard Code Conditions in UNIX Shell Script?

Hi All, I am trying to place one SQL query in Shell Script with Where Condition as Status='1' But after running the the script it is returning error as SQL0206N "1" is not valid in the context where it is used. SQLSTATE=42703 The query is working fine in Data Base. Please suggest... (1 Reply)
Discussion started by: sumanmca2006
1 Replies

3. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

4. Shell Programming and Scripting

Shell script executing both the conditions.

I have written this script. This is used for creating a backup folder. #!/bin/sh #set -x . /home/.profile usage="Usage is $0" usage="$usage " # Use the getopt utility to set up the command line flags. set -- `/usr/bin/getopt b: $*` # Process individual command line arguments while ;... (1 Reply)
Discussion started by: arijitsaha
1 Replies

5. Shell Programming and Scripting

Help With Expect Script IF with Multiple Conditions

I am needing to include some if statements within an expect script. These will need to have two conditions under an AND join. Upon a successful IF condition I want to set multiple variables. I have tried a lot of variations of the below statement with no success. I have also searched the WEB... (2 Replies)
Discussion started by: Slagathor
2 Replies

6. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

7. UNIX for Dummies Questions & Answers

multiple if conditions and EOF in a shell script

I want to create an IF condition with multiple condition, in the statement below I want to add OR EOF, can any one please advise how to do. if } != $sample ] && ; then echo ..... fi code tags please (1 Reply)
Discussion started by: analyst
1 Replies

8. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

9. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

10. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies
Login or Register to Ask a Question