Changes

Jump to: navigation, search

Tutorial 12 - Shell Scripting - Part 2

6,842 bytes added, 14:10, 14 November 2021
INVESTIGATION 3: exit AND break STATEMENTS
: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 <span style="font-family:courier;font-weight:bold;">exit</span> AND <span style="font-family:courier;font-weight:bold;">break</span> STATEMENTS = In this investigation, you will learn to use the '''exit''' and '''break''' statements in your shell scripts.  '''THE EXIT STATEMENT''' The '''exit''' statement is used to terminate a shell script.<br>This statement is very useful when combined with logic in a shell script to display an '''error message'''<br>if the command was '''improperly executed''' and '''terminate''' the running of the shell script.<br><br>The ''exit'' command can contain return a ''value'' to provide the '''exit status'''<br>of your shell script (i.e. TRUE or FALSE value).<br><br> '''Perform the Following Steps:''' # Make certain that you are logged into matrix account.<br><br># Confirm that you are currently located in the '''advanced''' directory.<br><br># Use a text editor like vi or nano to create the text file called '''exit.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi exit.bash</span>)<br><br># Enter the following lines in the '''exit.bash''' shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br><br>if [ $# -ne 1 ]<br>then<br> &nbsp; echo "USAGE: $0 [arg]" &gt;&amp;2<br> &nbsp; exit 1<br>fi<br><br>echo "The argument is: $1"</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 this Bash shell script.<br><br># Issue the following command (without arguments):<br><span style="color:blue;font-weight:bold;font-family:courier;">./exit.bash</span><br><br>What did you notice?<br><br>Since there are no arguments, the test within the running shell script returns FALSE,<br>then an '''error message''' with feedback of how to properly issue the shell script with an argument<br>and then '''terminates''' the Bash shell script.<br><br>Notice that the '''$0''' positional parameter displays the '''name''' of the currently running shell script<br>in the USAGE message. This is useful in case you decide to '''change''' the ''name'' of the shell script at a later time.<br><br>The symbol '''&gt;&amp;2''' redirects '''standard output''' from the USAGE message<br>to '''standard error''' making like a <u>real</u> error message.<br>This "''neat redirection trick''" will NOT be considered for evaluation for this coverage.<br><br># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $?</span><br><br>What does this '''exit status''' from the previously issued command indicate?<br><br># Issue the following command (with an argument):<br><span style="color:blue;font-weight:bold;font-family:courier;">./exit.bash osl640</span><br><br>What did you notice this time?<br><br># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $?</span><br><br>What does this '''exit status''' from the previously issued command indicate?<br><br># Issue the following command (with two arguments and redirecting stderr to a file):<br><span style="color:blue;font-weight:bold;font-family:courier;">./exit.bash osl640 Linux 2&gt; error.txt</span><br><br>What did you notice this time?<br><br># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $?</span><br><br># Issue the following Linux command to confirm that stderr was redirected to a file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat error.txt</span><br><br>  '''THE BREAK STATEMENT''' The '''break''' statement is used to '''terminate''' a '''loop''' <u>without</u><br>terminating the running shell script.<br><br> '''Perform the Following Steps:''' # Make certain that you are logged into matrix account.<br><br># Confirm that you are currently located in the '''advanced''' directory.<br><br># Use a text editor like vi or nano to create the text file called '''break-1.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi break-1.bash</span>)<br><br># Enter the following lines in the '''break-1.bash''' shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br><br>read -p "Enter an integer: " number<br><br>while ! echo $number | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null || [ $number -ne 5 ] 2> /dev/null<br>do<br> &nbsp; if [ $number -eq 5 ] 2> /dev/null<br> &nbsp; then<br> &nbsp; &nbsp; &nbsp;break<br> &nbsp; fi<br> &nbsp; read -p "Try again. Enter a valid integer: " number<br>done<br><br>echo "The number is: $number"</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 this Bash shell script.<br><br># Issue the following command (without arguments):<br><span style="color:blue;font-weight:bold;font-family:courier;">./break-1.bash</span><br><br>When prompted, enter several '''invalid''' and '''valid''' integers. Then enter '''valid integers''' NOT containing the value of '''5'''.<br>Finally, enter the integer with the value of '''5'''.<br><br>What happens?<br><br>Let's use the '''break''' statement with the '''for''' loop.<br><br># Use a text editor like vi or nano to create the text file called '''break-2.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi break-2.bash</span>)<br><br># Enter the following lines in the '''break-2.bash''' shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br><br>for x<br>do<br><br> &nbsp; if [ $x = "osl640" ]<br> &nbsp; then<br> &nbsp; &nbsp; &nbsp;break<br> &nbsp; fi<br> &nbsp; echo "Argument is: $x"<br>done<br><br>echo<br>echo "Shell script has been completed"</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 this Bash shell script.<br><br># Issue the following command (with arguments):<br><span style="color:blue;font-weight:bold;font-family:courier;">./break-2.bash nwk680 prg600 osl640 osm620</span><br><br>What do you notice? How come '''osl640''' and '''osm620''' are NOT displayed<br>but a message appeared at the end of the script that the script completed?<br><br># Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~osl640/week12-check-3</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><br> : In the next investigation, we will learn to create / modify '''startup files'''<br>to <u>customize</u> your Linux shell environment.
= LINUX PRACTICE QUESTIONS =

Navigation menu