Programming in bash - part 2

Second This mini-tutorial programming in Bash, where we learn to use ciclos and other tools that will help us improve the functionalities of our scripts.

The If conditional

The function of If is to apply a filter and perform an operation or task for each type of filter applied. Its structure is as follows:

If [condition]; then elif commands [condition]; then else commands; fi commands

An example is shown next to the For loop in its respective section.

Cycles

1. While: This loop will execute all the commands specified between do and done as long as the condition is true. If you want to pass comparison operators, the expression must be enclosed in square brackets.

while CONDITION / COMMAND do commands done

Example: multiplication table of a number

#! / bin / bash X = 1 echo "Enter a number and press ENTER" read M #The loop controls that X is less than or equal to 10 while [$ X –le 10] do #In R we store the multiplication of X by MR = $ [X * M] # This multiplication is printed on the screen echo "$ M * $ X = $ R" #With let, we increase the value of X by 1 unit let X = $ X + 1 done

2. For: stores a list of elements in a variable, which will be used to perform certain operations with each loop.

for VARIABLE in ELEMENTS do commands done

Example: program that copies a file from one folder to another, replacing an old file.

# / bin / bash #We establish source and destination directories ORIGIN = / home / user / Downloads DESTINATION = / home / user / Documents # We position ourselves in the source cd $ ORIGIN #Of all the files, we only want the one that is # call FILE for FILE in * do ARCH-DESTINATION = "$ DESTINATION / $ FILE" # -f filters us regular files, since directories are #nothing good for us. –Nt filters out # files “newer” than those #found in the destination folder if [-f $ FILE] && [$ FILE –nt $ ARCH-DESTINATION]; then echo "Copying $ FILE ..." # we copy the file with cp cp $ FILE $ ARCH-DESTINATION fi done #We cd to exit the source folder cd

Another example: script where the user must guess a random number generated by the computer.

# / bin / bash #A random number is generated from 1 to 10, it is # saved in RANDOM RANDOM = $ [$ RANDOM% 10 + 1] while [1] do echo –n "Enter a number:" read NUM # Compare if the number chosen by the user is #equal to RANDOM; remember to use $ to evaluate the #values ​​of the variables and not their names if [“$ NUM –eq“ $ RANDOM ”]; then echo "You got it right!" #break allows ending the loop while break #If the number is less than RANDOM elif [“$ NUM –gt“ $ RANDOM ”]; then echo "It is less" "If not, it is greater than RANDOM else echo" IT IS greater "fi done

3. Until: quite similar in structure to While, except that the code always executes while the condition is evaluated negatively, that is, the program continues to execute “until the condition occurs”

until CONDITION / COMMAND do commands done

Example: print the numbers 10-20 in descending order

#! / bin / bash CONT = 20 #As long as the counter is less than 10 (-lt, “lower #than”) the code is executed until [$ CONT -lt 10]; do echo "COUNTER $ CONT" #A CONT is subtracted by a unit let CONT- = 1 done

4. Select: This last loop is a special case, as it is generally used to quickly create menus. It works similarly to the For loop.

select VARIABLE in LIST of commands done

Example: give the user the ability to continue or end the script.

#! / bin / bash #item is a variable that responds to what the #user chooses, Continue and Finish are the #options available to him, although other options can be added # more select item in Continue Finish do #If the user chooses end the program, then with break we exit the cycle. if [$ item = "Finish"]; then break fi done
Thank you Juan Carlos Ortiz!

Add as preferred source in Google