Page 1 of 1

Posted: 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.

Posted: 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

Posted: 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.

Posted: 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/

Posted: 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

Posted: 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.

Posted: 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:

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

Posted: 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