Creating Bash Scripts if then else – Part 3
In the
previous pages we looked at some very basic Bash scripting, which involved outputting
text to the screen, getting a user’s input, storing it and outputting that to the
screen; as well as including a system variable using the Date command. Now let’s combine
what you’ve achieved so far and introduce Loops.
With
most programming structures there will come a time where you need to loop
through the commands you’ve entered to create
better functionality, and ultimately a better program.
Step
1
Let’s look at
the If, Then and Else statements now, which when
executed correctly, compare a set of
instructions
and simply work out that IF something is present, THEN do something,
ELSE do something different. Create a new script called greeting2.sh
and enter the text in the screenshot below
into it.
Step 2
Greeting2.sh
is a copy of greeting.sh but with a slight
difference. Here we’ve added a loop starting
at the if statement.
This means, IF the variable entered is equal to David the
next line, THEN, is the reaction to what happens, in this case it will
output to the screen ‘Awesome name,’ followed by the variable
(which is David).
Step 3
The next
line, ELSE, is what happens if the variable doesn’t equal
‘David’. In this case it simply outputs to the screen
the now familiar ‘Hello…’. The last line, the FI statement, is
the command that will end the loop. If you have an If command
without a Fi command, then you get an error.
Step 4
You can
obviously play around with the script a little,
changing the name variable that triggers a response; or
maybe even issuing a response where the first name and surname
variables match a specific variable.
More Looping
You can loop over data using the FOR, WHILE and UNTIL
statements. These can be handy if you’re batch naming, copying or running a script where a counter is needed.
Step 1
Create a new
script called count.sh. Enter the text in the
screenshot below, save it and make it executable.
This creates the variable ‘count’ which at the beginning of the script
equals zero. Then start the WHILE loop, which WHILE count is less
than (the LT part) 100 will print the current value of count in the
echo command.
Step 2
Executing the
count.sh script will result in the numbers 0 to
99 listing down the Terminal screen;
when it
reaches 100 the script will end. Modifying the script with the FOR
statement, makes it work in much the same way. To use it in our script, enter
the text from the screenshot into the count.sh script.
Step 3
The addition
we have here is: for count in {0..100}; do.
Which means: FOR the variable ‘count’ IN
the numbers from zero to one hundred, then start the loop. The
rest of the script is the same. Run this script, and the same output should
appear in the Terminal.
Step 4
The UNTIL
loop works much the same way as the WHILE loop
only, more often than not, in reverse.
So our
counting to a hundred, using UNTIL, would be: until [ $count -gt
100 ]; do. The difference being, UNTIL count is greater than
(the gt part) one hundred, keep on looping.
Step 5
You’re not
limited to numbers zero to one hundred. You can,
within the loop, have whatever set of commands you
like and execute them as many times as you want the loop to
run for. Renaming a million files, creating fifty folders etc. For
example, this script will create ten folders named folder1
through to
folder10 using the FOR loop.
Step 6
Using the FOR
statement once more, we can execute the
counting sequence by manipulating the {0..100}
part. This section of the code actually means {START..END.. INCREMENT},
if there’s no increment then it’s just a single digit up to the END. For
example, we could get the loops to count up to 1000 in two’s with: for
count in {0..1000..2}; do.
0 Response to "Creating Bash Scripts if then else – Part 3"
Post a Comment