Multiple If statements in bash problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple If statements in bash problem
# 1  
Old 10-18-2011
Multiple If statements in bash problem

Hi everyone,

May you help me with the correct syntax of the follow bash statements please
Code:
X=10
if [[ "$X" <= 5 ]]; then
    echo "The value is between 1 and 5"
    for ((i=1;i<=${X})); do
     echo $i
    done 
else if [[ "$X" > "5" && "$X"<= 10 ]]; then
    echo "The value is between 6 and 10"
    for ((i=1;i<=${X})); do
     echo $i
    done 
else if [[  "$X" > 10 )) &&  "$X" <= 20]; then
    echo "The value is between 10 and 20"
    for ((i=1;i<=${X})); do
     echo $i
    done 
fi

Thank in advance
# 2  
Old 10-18-2011
You have not mentioned the error messages.
At the first look,

for integer values comparison, You should you -lt type syntax.

Code:
-lt => less than
-le => less than or equal
-gt => greater than
-ge => greater than or equal
-eq => equal
-ne => not equal


There must be a white space on both side of comparison operator and square brackets.

e.g

Code:
[ "$X" <= 20] #invalid
["$X" <= 20 ] # invalid
[ "$X"-le 20] # invalid
[ "$X" -le 20 ] #valid


Last edited by clx; 10-18-2011 at 05:53 AM..
# 3  
Old 10-18-2011
Hi anchal_kare,

I receive syntax errors. I've tried both ways, like in my first post and using comparison operators as you suggest me.
Code:
X=10
if [[ "$X" -le 5 ]]; then
    echo "The value is between 1 and 5"
    for ((i=1;i<=${X})); do
     echo $i
    done 
else if [[ "$X" -gt 5 && "$X" -le 10 ]]; then
    echo "The value is between 6 and 10"
    for ((i=1;i<=${X})); do
     echo $i
    done 
else if [[  "$X" -gt 10 &&  "$X" -le 20 ]]; then
    echo "The value is between 10 and 20"
    for ((i=1;i<=${X})); do
     echo $i
    done 
fi

But I continue receiving syntax errors
-syntax error near of unexpected element "fi"
- it requires an arithmetic expression
- syntax error near of ";"

Thanks for your help
# 4  
Old 10-18-2011
And the error is that X is 10, and loop won't run since X never changes.

You have also loop errors
Code:
for ((i=1;i<=${X}));

Should be i guess
Code:
 for ((i=1;i<=${X}; i++));

Also a syntax error:
Code:
else if [[  "$X" > 10 ))

Guessing it should be
Code:
else if [ $X -gt 10 ] ..

# 5  
Old 10-18-2011
else if should be elif
This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 10-18-2011
Quote:
Originally Posted by itkamaraj
else if should be elif
Thanks itkamaraj,

Even when I had some other errors, that's was the main problem.

The correct syntax as I want is:
Code:
X=17
if (( "$X" <= 5 )); then
    echo "The value is between 1 and 5"
    for ((i=1;i<=${X};i++)); do
     echo $i
    done 
elif (( "$X" > 5 )) && (( "$X" <= 10 )); then
    echo "The value is between 6 and 10"
    for ((i=1;i<=${X};i++)); do
     echo $i
    done 
elif (( "$X" > 10 &&  "$X" <= 20 )); then
    echo "The value is between 10 and 20"
    for ((i=1;i<=${X};i++)); do
     echo $i
    done 
fi

Thanks again to all for your help.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using multiple 'for' statements

Hello, I am new to scripting and I am trying to write a simple script that creates users and adds their passwords from two files; one a user list file and another a password list file. For example, I have two files already. $ cat file1 andy stephane aby paul $ cat file2 123 234... (4 Replies)
Discussion started by: FemoTheDon
4 Replies

2. UNIX for Dummies Questions & Answers

Multiple if statements

exmaple: file1 and file2 has 1 bit if then exit else send out email fi if then exit else send out email fi it exits the 1st if. did not continue with my 2nd if. thanks in advanced. Please use CODE tags are suggested on every page when you post an item in... (7 Replies)
Discussion started by: lawsongeek
7 Replies

3. UNIX for Dummies Questions & Answers

Problem with multiple grep in bash loop

Hello, I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited): Sample1 Gene1 ## Gene2 ## Sample2 Gene2 ## Gene 4 ## Sample3 Gene3 ... (2 Replies)
Discussion started by: InfoSeeker2
2 Replies

4. Shell Programming and Scripting

Bash if statements in one line

I have the following bash code, and wondering if it is possible (and a good idea) to write the if statements in one line. # Run raytrac on the sound speed model. if ; then $bashPath/raytrac.bash -fcmd=$fcmd.cmd fi # Plot the travel times and ray paths. if ; then ... (8 Replies)
Discussion started by: kristinu
8 Replies

5. Shell Programming and Scripting

Multiple while statements?

I'm working on a script for class as a final project. We have to ask for values (city, state, zip) five times, but the state can only be MI, IN, IL, or OH. I'm trying to do this with a while loop inside of a while loop, but I have no idea how to do it properly. Here's what I have so far: ... (3 Replies)
Discussion started by: nickzourdos
3 Replies

6. Shell Programming and Scripting

Multiple if Statements

Hi All, I need to check for 3 conditions and if all the 3 are not satified need to say that services are not running.... is the below code correct. #********** Check to see if Service 1 is still running************** if then echo "$datetimestamp: Service1 is not running" >>... (4 Replies)
Discussion started by: ch33ry
4 Replies

7. Shell Programming and Scripting

Running multiple if statements

Hi, Hav automated a process, and it is working fine. for first process alone status = ` ps -ef |grep a ` if then echo " Success" > temp/logfile else echo " Failure" > temp/logfile fi Now I hav to write script to automated some 2 process and then send mail using cron.. ... (5 Replies)
Discussion started by: NehaKrish
5 Replies

8. Shell Programming and Scripting

How do I execute multiple statements within If then else

Please help me. I have been doing this for several hours. Here is the code if then echo a b c d >> file.txt echo 1111 >> file.txt fi The reason I want the two echo is because I want these statements printed on multiple lines. I keep getting error . First it tells me... (2 Replies)
Discussion started by: asemota
2 Replies

9. Shell Programming and Scripting

Errors in bash with if statements

Hello everyone, I got this type of error when programming in bash new.bat: 16: cannot create : Directory nonexistent $bool new.bat: 37: Syntax error: "then" unexpected (expecting "fi") Does anyone know why? Here is my code #!bin/bash #function helps(){... (4 Replies)
Discussion started by: bbbash
4 Replies

10. Shell Programming and Scripting

multiple conditions in if statements

Hi all, I'm confused about the proper syntax for multi-conditional if then statements. I'm trying to set limitations on info input on the command line.. i.e. if ] ;then $x=$vr1 else print "You have entered an invalid option." Can someone please clue me in on what is wrong with my syntax;... (3 Replies)
Discussion started by: tim mauger
3 Replies
Login or Register to Ask a Question