Bash 101 - to (do) ; or not to (do) ; ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash 101 - to (do) ; or not to (do) ; ?
# 1  
Old 08-28-2018
Bash 101 - to (do) ; or not to (do) ; ?

I figured this forum needs some laughs , so I am posting this.

And if the answer is - it depends on bash version - do not reply.



This is from "manual"


while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done

And here is the REAL code - no ";"


Code:
while [ $i -lt 4 ] do xterm & i=$[$i+1] done

# 2  
Old 08-28-2018
Quote:
Originally Posted by annacreek
Code:
while [ $i -lt 4 ] do xterm & i=$[$i+1] done

I don't know from which "manual" you got that, but there are more errors then correct words in it. Shame on whoever wrote this drivel. The correct form would be:

Code:
while [ $i -lt 4 ] ; do xterm & ; (( i = i+1 )) ; done

or, in a more readable way:

Code:
while [ $i -lt 4 ] ; do
     xterm &
     (( i = i+1 ))
done

Note that [ is actually a real command: it is a different way to invoke /bin/test. Written in "long form" where the command is more easily recognisable it would be:

Code:
while /bin/test $i -lt 4 ; do
     xterm &
     (( i = i+1 ))
done

The command

Code:
/bin/test $i -lt 4

will return 0 (logical TRUE) if the variable i is lower than 4 and 1 (logically FALSE) otherwise. The while-loop is repeated as long as the control-command (in this case /bin/test) returns 0 and stops if it returns 1. The following will read a complete file:

Code:
while read LINE ; do
     echo "== $LINE =="
done < /some/file

because the read-command returns 0 as long as it can read a line but returns 1 when it hits end-of-file.

I hope this helps.

bakunin
# 3  
Old 08-29-2018
A correction:
most shells don't like & ;. Only & was correct.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash 101 - string assigment

I am embarrassed to ask this but I am at the end of my rope. I am trying to assign one (string) variable to another. I do understand that bash variables are "not typed", but still learning usage of {} and spaces in script. I was hoping this syntax would work $variableA =... (6 Replies)
Discussion started by: annacreek
6 Replies

2. UNIX for Dummies Questions & Answers

Help Me please scripting 101

Hi, I had to pull a handful of account numbers from a file into a table. Now I want to do a basic list from a directory in my program showing me if any files for these customers exist. There are files associated with each client and need to be processed individually. $Paytos = 00153301 00153302... (5 Replies)
Discussion started by: ski
5 Replies

3. Shell Programming and Scripting

Daemon 101

I think I have an issue almost like Sammy_T's. I want to make a piece of code run as a daemon. I have some java, along with it 15 classpath's converted to a shell script that I can "runmyjavap". The script is just what I need to run after compiling it: #!/bin/sh java -classpath : ...(from... (3 Replies)
Discussion started by: Miller_K
3 Replies
Login or Register to Ask a Question