LinuxQuestions.org
Register a domain and help support LQ
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, 12:09 PM   #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 03:45 PM. Reason: Added CODE tags to improve readability.
 
Old 03-27-2013, 03:01 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,564
Blog Entries: 54

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

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 04:18 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,342
Blog Entries: 5

Rep: Reputation: 266Reputation: 266Reputation: 266
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, 06:09 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:04 AM
Script to Hit 'Enter' key multiple times in bash script. nithinmp Ubuntu 7 07-19-2012 08:01 AM
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 10:43 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
multiple if condition in bash tikit Programming 6 11-12-2008 02:34 PM


All times are GMT -5. The time now is 02:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration