You may execute a sequence of commands through writing them into a script file and then running it.
A script file is really a textual content file, generally with the .Sh document extension, that contains a sequence of instructions that would additionally be finished from the command line (shell).
At the same time as loop examples
Under is an instance of a while loop. Whilst performed, this script file will print the numbers 1 via 9 at the screen.
The whilst-assertion offers you greater flexibility for specifying the termination situation than the for-loop.
#!/bin/bash count=1 while [ $count -le 9 ] do echo "$count" sleep 1 (( count++ )) done
For example, you can make the preceding script an limitless loop with the aid of omitting the increment statement “(( matter++ ))”:
#!/bin/bash count=1 while [ $count -le 9 ] do echo "$count" sleep 1 done
The “sleep 1” announcement pauses the execution for 1 second on each generation. Use the ctrl+c keyboard shortcut to terminate the procedure.
You may also create an countless loop by way of placing a colon because the circumstance:
#!/bin/bash count=1 while : do echo "$count" sleep 1 (( count++ )) done
So as to use a couple of situations within the while-loop, you need to use the double rectangular bracket notation:
count=1 done=0 while [[ $count -le 9 ] && [ $done == 0 ]] do echo "$count" sleep 1 (( count++ )) if [ $count == 5 ]; then $done=1 fi done
In this script, the variable “done” is initialized to 0 after which set to 1 whilst the be counted reaches 5. The loop condition states that the even as loop will maintain so long as “count number” is much less than nine and “finished” is identical to 0. Therefore the loops exit whilst the remember equals 5.
The “&&” method logical “and” and “way logical “or”.
An opportunity notation for the conjunctions “and” and “or” in situations is “-a” and “-o” with single square brackets. The above circumstance:
[[ $count -le 9 ] && [ $done == 0 ]]
…May be rewritten as:
[ $count -le 9 ] -a [ $done == 0 ]
Analyzing a text report is commonly completed with some time loop. Inside the following instance, the bash script reads the contents line by line of a document referred to as “inventory.Txt:”
FILE=inventory.txt exec 6
The first line assigns the enter report name to the “record” variable. The second one line saves the “trendy enter” within the document descriptor “6” (it can be any cost between 3 and 9). This is completed so that “popular input” may be restored to report descriptor “zero” at the give up of the script (see the statement “exec 0 inside the 3rd line the input record is assigned to document descriptor “zero,” which is used for wellknown enter. The “examine” announcement then reads a line from the file on every new release and assigns it to the “line1” variable.
To be able to prematurely go out some time-loop, you could use the ruin assertion like this:
count=1 done=0 while [ $count -le 9 ] do echo "$count" sleep 1 (( count++ )) if [ $count == 5 ] then break fi done echo Finished
The break assertion skips program execution to the stop whilst loop and executes any statements following it.
In this example, the announcement “echo completed.”
The maintain declaration, alternatively, skips only the relaxation of the while loop announcement of the contemporary iteration and jumps at once to the following generation:
count=1 done=0 while [ $count -le 9 ] do sleep 1 (( count++ )) if [ $count == 5 ] then continue fi echo "$count" done echo Finished
In this situation, the “continue” announcement is achieved whilst the variable “remember” reaches five. This indicates the subsequent declaration (echo “$count”) isn’t carried out on this generation (whilst the cost of “depend” is 5).