"if" logic - please help

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
DMGrier

"if" logic - please help

Post by DMGrier »

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Otyugh

Re: "if" logic - please help

Post by Otyugh »

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

Re: "if" logic - please help

Post by DMGrier »

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.
Otyugh

Re: "if" logic - please help

Post by Otyugh »

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

Re: "if" logic - please help

Post by eanfrid »

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 :)
DMGrier

Re: "if" logic - please help

Post by DMGrier »

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
michelsberg

Re: "if" logic - please help

Post by michelsberg »

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:
[url]http://www.commandlinefu.com/commands/browse/sort-by-votes[/url]

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

Re: "if" logic - please help

Post by CaptainMark »

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
Locked

Return to “Scripts & Bash”