User Tools

Site Tools


ops102:bash_scripting_2_-_loops

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ops102:bash_scripting_2_-_loops [2024/03/14 13:12] chrisops102:bash_scripting_2_-_loops [2025/03/11 03:54] (current) – [tput Colour Codes] chris
Line 1: Line 1:
 ====== Bash Scripting 2: Loops ====== ====== Bash Scripting 2: Loops ======
  
-===== Video ===== 
- 
-[[https://seneca-my.sharepoint.com/:v:/g/personal/chris_tyler_senecapolytechnic_ca/EVD6JDdK6TFCi52TQAiY6C8BdoDDANaEi8P1pt3TH4xH8w?e=Up7yKF|Video Lecture]] 
  
 ===== Looping in Bash ====== ===== Looping in Bash ======
Line 59: Line 56:
 (It's also possible to generate the //LIST// in other ways -- for example, from the output of another command, captured with ''$( )'' ) (It's also possible to generate the //LIST// in other ways -- for example, from the output of another command, captured with ''$( )'' )
  
-=== The "C-style" for loop: for (( ; ; )) ===+==== The "C-style" for loop: for (( ; ; )) ====
  
 This loop is very similar to the "for" loop available in the C language. The C parenthesis are changed to bash double-parenthesis, to invoke the bash arithmetic syntax, and the curly-braces ''{ }'' used in C are replaced by the bash ''do'' and ''done'' keywords. The three arguments in the double-parenthesis are the initial condition (variable initialization), the control condition (an expression that, while true, causes the loop to continue), and the increment/decrement (an expression which is executed at the end of each loop, which typically increments or decrements a counter). This loop is very similar to the "for" loop available in the C language. The C parenthesis are changed to bash double-parenthesis, to invoke the bash arithmetic syntax, and the curly-braces ''{ }'' used in C are replaced by the bash ''do'' and ''done'' keywords. The three arguments in the double-parenthesis are the initial condition (variable initialization), the control condition (an expression that, while true, causes the loop to continue), and the increment/decrement (an expression which is executed at the end of each loop, which typically increments or decrements a counter).
Line 70: Line 67:
   done   done
  
-=== while EXPR ===+==== while EXPR ====
  
 This loop continues as long as the expression //EXPR// is true: This loop continues as long as the expression //EXPR// is true:
Line 81: Line 78:
 //EXPR// may be any single bash command, or a list of bash commands separated by newline characters (ENTER key) or semicolons, or a pipeline of commands. Most commonly it is a ''test'' ''<nowiki>[[</nowiki>'' command. //EXPR// may be any single bash command, or a list of bash commands separated by newline characters (ENTER key) or semicolons, or a pipeline of commands. Most commonly it is a ''test'' ''<nowiki>[[</nowiki>'' command.
  
-=== until EXPR ===+==== until EXPR ====
  
 This loop continues as long as the expression //EXPR// is false: This loop continues as long as the expression //EXPR// is false:
Line 92: Line 89:
 This loop will continue as long as ''$X'' is less than or equal to ''$Y'' -- as soon as the expression becomes true (when ''$X'' is greater than ''$Y''), the loop will stop. This loop will continue as long as ''$X'' is less than or equal to ''$Y'' -- as soon as the expression becomes true (when ''$X'' is greater than ''$Y''), the loop will stop.
  
-==== Examples ====+===== Examples =====
  
 Here are a couple of examples from the lecture: Here are a couple of examples from the lecture:
  
-=== tput Colour Codes ===+==== tput Colour Codes ====
  
 The ''tput'' command outputs terminal codes to stdout to perform actions such as setting the text colour, clearing the screen, and so forth.  The ''tput'' command outputs terminal codes to stdout to perform actions such as setting the text colour, clearing the screen, and so forth. 
Line 107: Line 104:
   do   do
   tput setaf $C   # Set foreground to colour $C   tput setaf $C   # Set foreground to colour $C
-  echo "This is colour $C"+  echo "Colour $C"
   done   done
   tput sgr0   # Reset to "normal" text mode   tput sgr0   # Reset to "normal" text mode
  
-=== Number-Guessing Game ===+==== Number-Guessing Game ====
  
 This is the main example from the lecture: This is the main example from the lecture:
Line 140: Line 137:
   SECRET=$(( RANDOM % MAX + 1 ))    SECRET=$(( RANDOM % MAX + 1 ))
      
-        # Un-commend this line when debugging!+        # Un-comment this line when debugging!
         # echo "NOTE: the secret number is $SECRET"         # echo "NOTE: the secret number is $SECRET"
      
ops102/bash_scripting_2_-_loops.1710421930.txt.gz · Last modified: 2024/04/16 18:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki