Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

1,200 bytes added, 08:22, 21 March 2021
INVESTIGATION 3: exit AND break STATEMENTS
# 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 '''exitbreak-1.bash''' shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br><br>read -p "Enter an integer: " number<br>while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> &nbsp; read -p "Try again. Enter a valid integer: " number<br> &nbsp; if [ $number -eq 5 ]<br> &nbsp; then<br> &nbsp; &nbsp; &nbsp;break<br> &nbsp; fi<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''' 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 that are '''NOT 5''', and then finally enter the value '''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-1.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>read -p "Enter an integer: " number<br>while ! echo $mark | egrep "^[0-9]{1,}$" > /dev/null 2> /dev/null<br>do<br> &nbsp; read -p "Try again. Enter a valid integer: " number<br> &nbsp; if [ $number -eq 5 ]<br> &nbsp; then<br> &nbsp; &nbsp; &nbsp;break<br> &nbsp; fi<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''' 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-2.bash</span><br><br>When prompted, enter several '''invalid''' and '''valid''' integers that are '''NOT 5''', and then finally enter the value '''5'''.<br><br>What happens?<br><br>
=INVESTIGATION 4: USING START-UP FILES =
13,420
edits

Navigation menu