"if" logic - please help

Forum rules
Before you post please read this

"if" logic - please help

Postby DMGrier on Sat Sep 07, 2013 12:23 am

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
User avatar
DMGrier
Level 1
Level 1
 
Posts: 26
Joined: Thu Mar 31, 2011 6:37 am

Linux Mint is funded by ads and donations.
 

Re: "if" logic - please help

Postby Otyugh on Sat Sep 07, 2013 4:59 am

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
Otyugh
Level 3
Level 3
 
Posts: 125
Joined: Wed Jan 02, 2013 9:52 pm

Re: "if" logic - please help

Postby DMGrier on Sat Sep 07, 2013 8:08 am

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
User avatar
DMGrier
Level 1
Level 1
 
Posts: 26
Joined: Thu Mar 31, 2011 6:37 am

Re: "if" logic - please help

Postby Otyugh on Sat Sep 07, 2013 8:26 am

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/
Otyugh
Level 3
Level 3
 
Posts: 125
Joined: Wed Jan 02, 2013 9:52 pm

Re: "if" logic - please help

Postby eanfrid on Sat Sep 07, 2013 8:59 am

Or without any "if":
Code: Select all
for count in 1 2 3 4
do [ $count -ne 3 ] && echo $count
done
... the power of the shell :)
Main desktop: Debian GNU/Linux Wheezy 64bit w/custom 3.14 longterm kernel - MATE 1.8.1
(i5 2400@3.7GHz - 16GB DDR3 - HD6770 w/radeon driver - SSD+RAID1)
True private storage on SpiderOak
User avatar
eanfrid
Level 7
Level 7
 
Posts: 1844
Joined: Mon Apr 30, 2012 2:49 am
Location: FR

Re: "if" logic - please help

Postby DMGrier on Sat Sep 07, 2013 5:07 pm

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. :D
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
User avatar
DMGrier
Level 1
Level 1
 
Posts: 26
Joined: Thu Mar 31, 2011 6:37 am

Re: "if" logic - please help

Postby michelsberg on Tue Oct 15, 2013 7:08 pm

eanfrid wrote:Or without any "if":
Code: Select all
for count in 1 2 3 4
do [ $count -ne 3 ] && echo $count
done
... the power of the shell :)


or
Code: Select all
for count in 1 2 3 4
do [ $count -eq 3 ] || echo $count
done

or
Code: Select all
for count in 1 2 3 4
do [ $count == 3 ] || echo $count
done

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/
michelsberg
Level 1
Level 1
 
Posts: 12
Joined: Fri Aug 12, 2011 4:08 am
Location: Germany

Re: "if" logic - please help

Postby CaptainMark on Sat Oct 26, 2013 2:55 pm

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
CaptainMark
Level 2
Level 2
 
Posts: 91
Joined: Mon Nov 28, 2011 8:19 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests