Introducing control statements
Control statements, including conditional statements and loops, are an integral part of shell scripting, allowing you to incorporate decision-making and repetitive tasks in your scripts. As a data scientist, you might use control statements when automating data preprocessing, running different analyses based on certain conditions, or when building complex pipelines. This section will introduce the most commonly used control statements in Bash scripting.
Just like other programming languages, Bash provides conditional statements to control the flow of execution. The most common conditional statements in Bash are if
, if-else
, and if-elif-else
.
Let’s take a look at a simple if
statement:
#!/bin/bash x=10 if [ $x -gt 5 ] then   echo "x is greater than 5" fi
In this script, if the value of x
is greater than 5, the message x is greater than 5
is printed to the console.
As you can see, control statements are often paired...