Difference between revisions of "Tutorial12: Shell Scripting - Part 2"
(→INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS) |
(→INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS) |
||
Line 148: | Line 148: | ||
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br> | # Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br> | ||
# Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-3.bash</span><br><br> | # Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-3.bash</span><br><br> | ||
− | # Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-3.bash</span><br><br>What do you notice? How does the result differ from the shell script called for-2.bash. Why?<br><br> | + | # Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-3.bash</span><br><br>What do you notice? How does the result differ from the shell script called for-2.bash. Why?<br><br>Let's create another shell script to run a loop for each file that is contained in your current directory using command substitution.<br><br> |
− | + | # Use a text editor like vi or nano to create the text file called '''for-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-4.bash</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br> | |
− | # Use a text editor like vi or nano to create the text file called '''for- | + | # Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>set $(ls)<br>echo Here are files in my current directory:"<br>for x<br>do<br> echo " $x"<br>done<br></span><br><br> |
− | # Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>set | ||
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br> | # Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br> | ||
− | # Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for- | + | # Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-4.bash</span><br><br> |
− | # Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for- | + | # Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-4.bash</span><br><br>What do you notice?<br><br>We can save a line in our shell script by using command substitution in the for loop using a list. Let's demonstration this in another shell script.<br><br> |
+ | # Use a text editor like vi or nano to create the text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-5.bash</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br> | ||
+ | # Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>echo Here are files in my current directory:"<br>for x in $(ls)<br>do<br> echo " $x"<br>done<br></span><br><br> | ||
+ | # Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br> | ||
+ | # Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-5.bash</span><br><br> | ||
+ | # Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-5.bash</span><br><br>What do you notice? Does the output for this shell script differ than for-4.bash? Why?<br><br>The last thing in this section is to introduce you to error-checking.<br><br> | ||
# x | # x |
Revision as of 08:26, 29 July 2020
Contents
ADDITIONAL SHELL SCRIPTING
Main Objectives of this Practice Tutorial
- Learn about additional logic control-flow statements if-else and if-elif-else
- Learn additional uses of the for loop control-flow statement.
- Learn the purpose of command substitution and how it can be used with control-flow statements
- List and explain the purpose of several start-up files
Tutorial Reference Material
Course Notes |
Linux Command/Shortcut Reference |
YouTube Videos | ||
Course Notes:
|
Additional Control Flow Statements | Startup Files
|
Brauer Instructional Videos: |
KEY CONCEPTS
Additional Logic Statements
If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.
If the test returns a FALSE value, then the the Linux Commands between
the else and fi statements are executed.
Example:
num1=5
num2=10
if test $num1 –lt $num2
then
echo “Less Than”
else
echo “Greater Than or Equal to”
fi
If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.
If the test returns a FALSE value, then a new condition is tested,
and action is taken if the result is TRUE . Eventually, an action will be taken
when the final test condition is FALSE
Example:
num1=5
num2=10
if test $num1 –lt $num2
then echo “Less Than”
elif test $num1 –gt $num2
then
echo “Greater Than”
else echo “Equal to”
fi
Additional Loop Statements
Command Substitutioncommand substitution is a facility that allows a command
to be run and its output to be pasted back on the command line as arguments to another command.
Reference: https://en.wikipedia.org/wiki/Command_substitution
Examples:
command1 $(command2)
command1 [arguments from command2 output]
for Loop using Command Substitution
Let’s issue the for loop with a list using command substitution.
In the example below, we will use command substitution to issue the ls command and
have that output (filenames) become arguments for the for loop.
Example:
for x in $(ls)
do
echo “The item is: $x”
done
The condition/expression is evaluated, and if the condition/expression is true,
the code within … the block is executed.
This repeats until the condition/expression becomes FALSE.
Reference: https://en.wikipedia.org/wiki/While_loop
Example:
answer=10
read –p “pick a number between 1 and 10: “ guess
while test $guess –eq 10
do read –p “Try again: “ guess
done
echo “You are correct”
Using Startup Files
Shell configuration files are scripts that are run when you log in, log out, or start a new shell. The start-up files can be used, for example, to set the prompt and screen display, create local variables, or create temporary Linux commands (aliases)
The /etc/profile file belongs to the root user and is the first start-up file that executes when you log in, regardless of shell.
User-specific config start-up files are in the user's home directory: ~/.bash_profile runs when you log in ~/.bashrc runs when you start an interactive sub-shell.
Logout Files
There are files that reset or restore the environment or properly shut-down running programs when the user logs out of their shell. User-specific logout start-up files are in the user's home directory: ~/.bash_logout
INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS
In this section, you will learn additional control-flow statements to allow your shell scripts to be more flexible.
Perform the Following Steps:
- Login your matrix account.
- Issue a command to confirm you are located in your home directory.
In a previous tutorial, you learned how to using the if control-flow statement. You will now learn to use the if-else statement
to take two different actions based on if the condition tests either TRUE OR FALSE. - Use a text editor like vi or nano to create the text file called if-3.bash (eg. vi if-3.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
if [ $num1 -gt $num2 ]
then
echo "The first number is greater than the second number."
else
echo "The first number is less than or equal to the second number."
fi - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
- Issue the following linux command to add execute permissions for your shell script:
chmod u+x if-3.bash - Run your shell script by issuing: ./if-3.bash
What do you notice? Try running the script several times with numbers different and equal to each other to
confirm that the shell script works correctly.
It would be nice to have a separate result of the numbers are equal to each other.
In order to achieve this, you can use an if-elif-else statement. - Use a text editor like vi or nano to create the text file called if-4.bash (eg. vi if-4.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
if [ $num1 -gt $num2 ]
then
echo "The first number is greater than the second number."
elif [ $num1 -gt $num2 ]
then
echo "The first number is less than the second number."
else
echo "The first number is equal to the second number."
fi - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
NOTE: You should notice in this control-flow statement differs from an if-else statement since if the first condition is FALSE, it is tested again, which can produce two different actions depending if the second test is TRUE or FALSE. - Issue the following linux command to add execute permissions for your shell script:
chmod u+x if-4.bash - Run your shell script by issuing: ./if-4.bash
What do you notice? Try running the script several times with numbers different and equal to each other
to confirm that the shell script works correctly.
A classic shell script to demonstrate many different paths or actions to take depending of multiple testing
using an if-elif-else statement would be a mark to letter grade converter. - Use a text editor like vi or nano to create the text file called if-5.bash (eg. vi if-5.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
read -p "Enter a mark (0-100): " mark
if [ $mark -ge 80 ]
then
echo "You receive an A grade."
elif [ $mark -ge 70 ]
then
echo "The receive a B grade."
elif [ $mark -ge 60 ]
then
echo "The receive a C grade."
elif [ $mark -ge 50 ]
then
echo "The receive a D grade."
else
echo "You receive an F grade."
fi - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
- Issue the following linux command to add execute permissions for your shell script:
chmod u+x if-5.bash - Run your shell script by issuing: ./if-5.bash
What do you notice? Run several times to confirm that the shell script runs correctly for all mark (grade) categories.
- In the next investigation, you will learn more about the for loop and learn how to use the while loop for error-checking.
INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS
In this section, you will learn more about the for loop and learn how to use the while loop for error-checking.
Perform the Following Steps:
- Use the more command to view the contents of the text file called for-1.bash (eg. more for-1.bash)
As you should have noticed from tutorial 10 that the for loop can use a list.
You can also use the for loop with positional parameters stored as arguments from an executed shell script.
We will revisit this now. - Use the more command to view the text file called for-2.bash (eg. more for-2.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Run your shell script by issuing: ./for-2.bash 10 9 8 7 6 5 4 3 2 1
You should notice the script looped for each argument following the shell script.
You can also use the for loop with a list using command substitution - this is an effective technique to loop within a shell script. - First, you need to learn how to use command substitution to store arguments as positional parameters.
Issue the following linux command to set positional parameters in your current shell:
set apples oranges bananas pears - Issue the following linux command:
echo $#
What do you notice? - Issue the following linux command:
echo $*
What do you notice?
These positional parameters could be used with a for loop. To simplify things, let's create another shell script that uses command substitution and a for loop. - Use a text editor like vi or nano to create the text file called for-2.bash (eg. vi for-3.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
set 10 9 8 7 6 5 4 3 2 1
for x
do
echo $x
done
echo "blast-off!" - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
- Issue the following linux command to add execute permissions for your shell script:
chmod u+x for-3.bash - Run your shell script by issuing: ./for-3.bash
What do you notice? How does the result differ from the shell script called for-2.bash. Why?
Let's create another shell script to run a loop for each file that is contained in your current directory using command substitution. - Use a text editor like vi or nano to create the text file called for-4.bash (eg. vi for-4.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
set $(ls)
echo Here are files in my current directory:"
for x
do
echo " $x"
done - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
- Issue the following linux command to add execute permissions for your shell script:
chmod u+x for-4.bash - Run your shell script by issuing: ./for-4.bash
What do you notice?
We can save a line in our shell script by using command substitution in the for loop using a list. Let's demonstration this in another shell script. - Use a text editor like vi or nano to create the text file called for-5.bash (eg. vi for-5.bash)
If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule. - Enter the following lines in your shell script:
#!/bin/bash
clear
echo Here are files in my current directory:"
for x in $(ls)
do
echo " $x"
done - Save your editing session and exit the text editor (eg. with vi: press ESC, then type :wx followed by ENTER).
- Issue the following linux command to add execute permissions for your shell script:
chmod u+x for-5.bash - Run your shell script by issuing: ./for-5.bash
What do you notice? Does the output for this shell script differ than for-4.bash? Why?
The last thing in this section is to introduce you to error-checking.
- x
In the next investigation, you will ...
INVESTIGATION 3: USING STARTUP FILES
In this section, you will learn how to ...
Perform the Following Steps:
- x
- After you complete the Review Questions sections to get additional practice, then work on your
online assignment 3.
LINUX PRACTICE QUESTIONS
The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.
Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week12_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
Review Questions:
- x
- x
- x
- x
- x
- x
- x
- x