Changes

Jump to: navigation, search

Tutorial12: Shell Scripting - Part 2

1,363 bytes added, 08:21, 21 March 2021
INVESTIGATION 3: exit AND break STATEMENTS
# '''Login''' to your matrix account.<br><br>
# Issue a Linux command to <u>confirm</u> you are located in the '''advanced''' directory.<br><br>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<br>a '''message''' if the command was '''not run correctly''' and terminate the running of the shell script.<br><br>The ''exit'' command can contain an argument to provide the '''exit status'''<br>of your shell script (i.e. TRUE or FALSE value).<br><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>
# Enter the following lines in your 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>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, then an '''error message''' with feedback of how to properly issue the shell script with an argument and terminates the Bash shell script.<br><br>The symbol &gt;&amp;2 redirects standard output from the USAGE message as standard error making like a real error message.<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>
=INVESTIGATION 4: USING START-UP FILES =
13,420
edits

Navigation menu