LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2013, 11:09 AM   #1
grima
LQ Newbie
 
Registered: Mar 2013
Posts: 1

Rep: Reputation: Disabled
multiple if condition in a bash script


Hi,

I've a question concerning multiple if 'conditions' in bash.

My code looks as follows:

Code:
#!/bin/bash
for i in {1..20..1}
do

  if [[ $i -eq {7 || 11 || 18} ]]
  then
   echo "Welcome $i times"
  fi

done
For example, the 'echo' in the if condition should be for the numbers 7,
11, 18.
But it doesn't do.

Does somebody know how to do that?

Best, grima

Last edited by colucix; 03-27-2013 at 02:45 PM. Reason: Added CODE tags to improve readability.
 
Old 03-27-2013, 02:01 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Why not use a case statement: 'case $i in 7|11|18) echo "Welcome $i times";; esac;'?
 
Old 03-27-2013, 02:45 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-28-2013, 03:18 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Hi and welcome to LQ!

Try this:

Code:
#!/bin/bash
for i in {1..20..1}
do

  if [[ $i -eq 7 || $i -eq 11 || $i -eq 18 ]]
  then
   echo "Welcome $i times"
  fi

done
 
2 members found this post helpful.
Old 03-28-2013, 05:09 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you only need to match exact number strings, I second colucix's recommendation for a case statement. They're almost always more efficient than if tests.
Code:
#!/bin/bash
for i in {1..20}; do

    case i in
        7|11|18) echo "Welcome $i times" ;;
    esac

done
But if you need to do other numerical comparisons, then a bash "((..))" arithmetic operator is the best choice.

Code:
#!/bin/bash
for i in {1..20}; do
    if (( i == 7 || i == 11 || i >= 18 )); then
        echo "Welcome $i times"
    fi
done
When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression


And two other minor points:

1) You don't need a step operator in brace expansion if you're just counting by ones anyway.

2) Many scripters also feel that it's better to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as they are not separate commands but are paired with the opening keyword to bracket the test/input string. Putting them together on one line thus helps to better visually separate the outside block from the inside block.

Scripting With Style
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] multiple iterations of the same variable in a BASH script Regnets1 Programming 3 02-11-2013 07:04 AM
Script to Hit 'Enter' key multiple times in bash script. nithinmp Ubuntu 7 07-19-2012 07:01 AM
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 09:43 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
multiple if condition in bash tikit Programming 6 11-12-2008 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration