Open main menu

CDOT Wiki β

Changes

Tutorial12: Shell Scripting - Part 2

3,090 bytes added, 09:07, 30 July 2020
INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-3.bash</span><br><br>What do you notice? Try running the script several times with numbers different and equal to each other to<br>confirm that the shell script works correctly.<br><br>It would be nice to have a separate result of the numbers are equal to each other.<br>In order to achieve this, you can use an '''if-elif'''-else statement.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-4.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-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>
# Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clear<br>read -p "Enter the first number: " num1<br>read -p "Enter the second number: " num2<br>if [ $num1 -gt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is greater than the second number."<br>elif [ $num1 -gt lt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is less than the second number."<br>else<br>&nbsp;&nbsp;&nbsp;echo "The first number is equal to the second number."<br>fi</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>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.<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 if-4.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>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>
# 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>echo<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$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-4.bash</span><br><br>
# 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>echo<br>for x in $(ls)<br>do<br>&nbsp;&nbsp;&nbsp;echo " &nbsp;&nbsp;&nbsp;$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>
# Use the '''more''' command to view the text file called '''if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more if-5.bash</span>)<br><br>Take a few moments to re-familiarize yourself with this shell script<br><br># xRun your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash </span><br><br>When prompted, enter a letter instead of a number. What happens?<br><br>Let's edit the if-5.bash shell script to perform error-checking to force the user to enter a numeric value between 0 and 100.<br><br>'''NOTE:''' The '''while''' statement can be used with the '''test''' command (or a simple linux command or a linux pipeline command) for error checking. In our case, we will use a pipeline command with extended regular expressions. In order to loop while the result is TRUE (not FALSE), you can use the negation symbol (!) to set the test condition to the opposite.<br><br># Use a text editor like vi or nano to edit the text file called '''if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-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># Add the following lines in your shell script <u>AFTER</u> the read statement to prompt the user for a mark:<br><span style="font-family:courier;"><br>while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> &nbsp;&nbsp;&nbsp;read -p "Not a valid number. Enter a mark (0-100): " mark<br>done</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># Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash</span><br><br># When prompted, enter a letter instead of a number. What happens?<br>Does the shell script allow you to enter an invalid grade like 200 or -6?<br><br>Let's add an additional error-checking loop to force the user to enter a number between 0 and 100.<br><br># Use a text editor like vi or nano to edit the text file called '''if-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-5.bash</span>)<br><br># Add the following lines in your shell script <u>AFTER</u> the PREVIOUSLY ADDED error-checking code block to force the user to enter a valid number:<br><span style="font-family:courier;"><br>while [ $mark -lt 0 ] || [ $mark -gt 100 ]<br>do<br> &nbsp;&nbsp;&nbsp;read -p "Invalid number range. Enter a mark (0-100): " mark<br>done</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># Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-5.bash</span><br><br># When prompted, enter a letter instead of a number. What happens?<br>Does the shell script allow you to enter an invalid grade like 200 or -6?<br><br>:In the next investigation, you will ..learn to create and test-out start-up files to customize your shell.
=INVESTIGATION 3: USING STARTUP FILES =
13,420
edits