Changes

Jump to: navigation, search

Tutorial 12 - Shell Scripting - Part 2

11,033 bytes added, 14:07, 14 November 2021
INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS
= INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS =
In this investigation, you will learn more about the '''for''' loop<br>and learn how to use the '''while''' loop for '''error-checking'''.
 
 
'''Perform the Following Steps:'''
 
# Issue a Linux command to <u>confirm</u> you are located in your '''advanced''' directory in your Matrix account.<br><br>
# Issue the following Linux command to view the <span style="font-family:courier;font-weight:bold;">~/for-1.bash</span> file:<br><span style="color:blue;font-weight:bold;font-family:courier;">more ~/for-1.bash</span>)<br><br>As you should have noticed from '''tutorial 10''' that the '''for''' loop can use a '''list'''.<br>You can also use the for loop with positional parameters stored as '''arguments'''<br>from an executed shell script.<br><br>You can also use the '''for''' loop with a list using '''command substitution'''.<br>Using command sustitution is an effective method to loop within a shell script.<br><br>Before creating a new shell script, let's learn to use command substitution from the Bash Shell<br>to store arguments as positional parameters and use them for practice.<br><br>
# Issue the following linux command to set positional parameters in your current shell:<br><span style="color:blue;font-weight:bold;font-family:courier;">set apples oranges bananas pears</span><br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $#</span><br><br>What do you notice? What does this value represent?<br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $*</span><br><br>What do you notice?<br><br>These positional parameters can be used with a for loop. To simplify things,<br>let's create another shell script that uses '''command substitution''' within a '''for''' loop.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-3.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-3.bash</span>)<br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>clear<br>set 10 9 8 7 6 5 4 3 2 1<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo $x<br>done<br>echo "blast-off!"</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for the owner of this script and '''run this Bash shell script'''.<br><br>What do you notice?<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>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/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</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' and '''run''' this Bash shell script.<br><br>What do you notice?<br><br>We can reduce a line in our shell script by using '''command substitution''' in the for loop instead of using<br>the '''set''' command. Let's demonstrate 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>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/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>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# '''Add execute permissions''' for this shell script and '''run Bash shell script'''<br>What do you notice? Does the output for this shell script differ from '''for-4.bash'''? Why?<br><br>We now want to introduce you to the use of '''error-checking'''.<br><br>
# Use the '''more''' command to view the previously-created Bash shell script '''~/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>
# Run 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''' <u>instead</u> of a ''number''. What happens?<br><br>Let's edit the '''for-5.bash''' shell script to perform '''error-checking''' to <u>force</u> 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>
# Add the following lines in your shell script <u>IMMEDIATELY AFTER</u> the read statement to prompt the user for a mark:<br><span style="font-family:courier;font-weight:bold;">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 ''':x''' 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''' <u>instead</u> 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>Compound operators like '''&&''' and '''||''' can be used with the '''test''' command.<br>Let's use the '''||''' compound criteria to to NOT accept numbers '''outside''' of the range '''0''' to '''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>IMMEDIATELY AFTER</u> the PREVIOUSLY ADDED<br>error-checking '''while''' loop statement to '''force''' the user to enter a valid number (between 1 and 100):<br><span style="font-family:courier;font-weight:bold;">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 ''':x''' 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''' <u>instead</u> 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 reinforce '''math operations''' in a shell script (that you created in '''tutorial 10''') and then incorporate math operations within a loop.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-6.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-6.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-weight:bold;font-family:courier;"><br>#!/bin/bash<br>value=1<br>while [ $value -le 5 ]<br>do<br>&nbsp;&nbsp;echo "$value"<br>&nbsp;&nbsp;value=value+1<br>done<br></span><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# Set execute permissions for this shell script and run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-6.bash</span><br><br>'''You should have noticed an error message'''.<br><br>
# To demonstrate what went wrong, <u>issue</u> the following '''commands''':<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>result=$num1+$num2<br>echo $result<br><br></span>Notice that the user-defined variable stores the text "'''10+5'''" which is <u>NOT</u> the expected result of adding the number '''10''' and '''5'''.<br><br>As you may recall in '''tutorial 10''', we need to convert a number stored as text into a '''binary number'''<br>for calculations (in this case, advance the value by 1 for each loop).<br>We can accomplish this by using the math construct '''(( ))'''<br><br>
# To demonstrate, <u>issue</u> the following set of '''commands''':<br><br><span style="color:blue;font-weight:bold;font-family:courier;">num1=5;num2=10<br>sum=$(($num1+$num2))<br>echo $sum<br><br>((product=$num1*$num2))<br>echo $product</span><br><br>Let's correct our '''for-6.bash''' shell script to correctly use math operations.<br><br>
# Use a text editor like vi or nano to edit the text file called '''for-6.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-6.bash</span>)<br><br>
# Edit '''line 6''' and replace with the following:<br><span style="font-family:courier;font-weight:bold;">((value=value+1))</span><br><br>'''Note:''' For those familiar with other programming languages, you can achieve the same results by using: '''((value++))'''<br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':x''' followed by '''ENTER''').<br><br>
# '''Run''' this Bash shell script again.<br><br>What do you notice this time? <br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~osl640/week12-check-2</span><br><br>
# If you encounter errors, make corrections and '''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<br><br>
:In the next investigation, you will learn to use the '''exit''' statement to '''terminate the execution of a shell script'''<br>if not run with the properly number of arguments and use the '''break''' statement that will '''terminate a loop'''<br>but NOT terminate the running of the shell script.
= INVESTIGATION 3: USING STARTUP FILES =
= LINUX PRACTICE QUESTIONS =

Navigation menu