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.
"if" logic - please help
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
"if" logic - please help
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: "if" logic - please help
"-ne" stands for "not equal".for count in 1 2 3 4
do
if [ $count -ne 3 ]
then
echo $count
fi
done
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
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.
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/
Practice is the key o/
Re: "if" logic - please help
Or without any "if":
... the power of the shell
Code: Select all
for count in 1 2 3 4
do [ $count -ne 3 ] && echo $count
done
Re: "if" logic - please help
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.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/
Re: "if" logic - please help
oreanfrid wrote:Or without any "if":... the power of the shellCode: Select all
for count in 1 2 3 4 do [ $count -ne 3 ] && echo $count done
Code: Select all
for count in 1 2 3 4
do [ $count -eq 3 ] || echo $count
done
Code: Select all
for count in 1 2 3 4
do [ $count == 3 ] || echo $count
done
Code: Select all
for count in {1..4}
...
[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]
Re: "if" logic - please help
orThat'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
Code: Select all
for count in 1 2 3 4; do
if [ $count -eq 3 ]; then
continue;
else
echo $count;
fi;
done