## "if" logic - please help

Forum rules
Before you post please read how to get help
DMGrier
Level 1
Posts: 25
Joined: Thu Mar 31, 2011 6:37 am

### "if" logic - please help

So I am trying to figure out how to use "if" logic on this:
"for count in 1 2 3 4
do
echo \$count
done"
so the system shows me it count to four when I run this simple script, I am trying to use "if" logic to get it to skip the number three when it counts, I tried finding guides and I have been playing with this for about two hours, can someone help me with this.
Sony Vaio EB series
OS: Linux Mint 15 Olivia 64 Bit
CPU: Intel i5 2.53 Ghz
RAM: 4 GB DDR3 1066 Mhz
HDD: 500 GB 5400 RPM
GPU: Intel HD

Otyugh
Level 3
Posts: 120
Joined: Wed Jan 02, 2013 9:52 pm

### Re: "if" logic - please help

for count in 1 2 3 4
do
if [ \$count -ne 3 ]
then
echo \$count
fi
done

"-ne" stands for "not equal".
I could have used "-eq" but I should have used a "!" to inverse the equal.

Here I have used the "number way of thinking". But "3" is also a character, and I could have used "=" to indetify the "3" number as a char (effects are the same here, but it's not the same way of doing).

>http://tldp.org/LDP/abs/html/comparison-ops.html

Or simply use the "if <condition> then <this thing> else <this other thing> fi" structure. You should find plenty of tutorial on the web. Strong base is the best, and mine are pretty corroded by the time of "not using". Maybe this is a very dumb code, but i don't see how doing it elseway :O

DMGrier
Level 1
Posts: 25
Joined: Thu Mar 31, 2011 6:37 am

### Re: "if" logic - please help

Thanks so much, I just could not figure this out. I found some guides and and worksheets I am using to try and learn working in the command line better.I do get stuck here and there and thank you for your help, it worked great and I understand what you did, thanks so much.
Sony Vaio EB series
OS: Linux Mint 15 Olivia 64 Bit
CPU: Intel i5 2.53 Ghz
RAM: 4 GB DDR3 1066 Mhz
HDD: 500 GB 5400 RPM
GPU: Intel HD

Otyugh
Level 3
Posts: 120
Joined: Wed Jan 02, 2013 9:52 pm

### Re: "if" logic - please help

I'm always glad to help ! I hope there will be another future bash question (they are quite rare), if not I'll forget the syntaxe myself ^^
Practice is the key o/

eanfrid
Level 7
Posts: 1861
Joined: Mon Apr 30, 2012 2:49 am
Location: FR

### Re: "if" logic - please help

Or without any "if":

Code: Select all

`for count in 1 2 3 4do [ \$count -ne 3 ] && echo \$countdone`
... the power of the shell
Main desktop: Debian GNU/Linux Jessie 64bit - MATE
(i5 2400@3.7GHz - 16GB DDR3 - HD6770 w/radeon driver - SSD+RAID1)
Safer than Dropbox

DMGrier
Level 1
Posts: 25
Joined: Thu Mar 31, 2011 6:37 am

### Re: "if" logic - please help

Otyugh wrote:I'm always glad to help ! I hope there will be another future bash question (they are quite rare), if not I'll forget the syntaxe myself ^^
Practice is the key o/

There will be, at least from me, I am trying to learn bash and I have quiet a few guides and question worksheets, so if I get lost I will turn to the community.
Sony Vaio EB series
OS: Linux Mint 15 Olivia 64 Bit
CPU: Intel i5 2.53 Ghz
RAM: 4 GB DDR3 1066 Mhz
HDD: 500 GB 5400 RPM
GPU: Intel HD

michelsberg
Level 1
Posts: 12
Joined: Fri Aug 12, 2011 4:08 am
Location: Germany

### Re: "if" logic - please help

eanfrid wrote:Or without any "if":

Code: Select all

`for count in 1 2 3 4do [ \$count -ne 3 ] && echo \$countdone`
... the power of the shell

or

Code: Select all

`for count in 1 2 3 4do [ \$count -eq 3 ] || echo \$countdone`

or

Code: Select all

`for count in 1 2 3 4do [ \$count == 3 ] || echo \$countdone`

or (at least in BASH)

Code: Select all

`for count in {1..4}...`

Some excellent command line snippets can be found here:
http://www.commandlinefu.com/commands/browse/sort-by-votes

This may help to understand some commands (but it still has a lot of glitches):
http://explainshell.com/

CaptainMark
Level 3
Posts: 105
Joined: Mon Nov 28, 2011 8:19 am

### Re: "if" logic - please help

or

Code: Select all

`for count in 1 2 3 4; do     if [ \$count -eq 3 ]; then         continue;     else         echo \$count;     fi; done`
That's the good part about scripting you can do it how you like, note the indentations that can make your scripts loops and if statements much easier to read, there is no right or wrong, but pick your style and stick with it, you will make your longer scripts much easier to read
Regards
Mark

Return to “Scripts & Bash”

### Who is online

Users browsing this forum: No registered users and 2 guests