Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

21 bytes added, 09:06, 18 March 2021
INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS
# 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 ''':wx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions ''' for this shell script and '''run the Bash shell script'''<br>What do you notice? Does the output for this shell script differ than '''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 text file called '''for-5.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">more for-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;">./for-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>
# Edit '''line 6''' and change to 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 ''':wx''' followed by '''ENTER''').<br><br>
# '''Add execute permissions ''' for the owner of this script file and '''run this Bash shell script'''.<br>What do you notice? <br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts/week12-check-1</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>
13,420
edits

Navigation menu