for variable in list-of-variables do commands doneFor example, both of the following scripts print the numbers 1, 2, 3, and 4.
#!/bin/bash # Steve Dannelly # demo for loops for index in 1 2 3 4 do echo $index done |
while [ condition ] do commands doneFor example, the following script also prints the numbers 1, 2, 3, and 4.
#!/bin/bash # Steve Dannelly # demo of while loops index=1 while [ $index -lt 5 ] do echo $index index=`expr $index + 1` done |
Those strange backward single quotes, which are difficult to find on the keyboard, perform an operation, then returns the value of the operation. So, linecnt=`cat myfile | wc -l` fill the variable linecnt with the number of lines in the file myfile.
Your new script will likely be much shorter, which will make it easier to edit for the assignment after this one.