Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

2,653 bytes added, 2 March
Using Variables in Shell Scripts
:* Explain the purpose of '''control flow statements'''.
:* Explain the purpose of the '''$?''' exit status and the '''test''' command.
:* Explain the purpose and usage of the '''if''' and '''if-else''' logic statement statements. :* Explain the purpose and usage of the '''for''' loop statement.<br><br>
===Tutorial Reference Material===
* [ Environment]
* [,like%20a%20real%20computer%20program. User Defined]
* [,up%20to%20nine%20positional%20parameters. Positional Parameters]Commands/ Techniques
* [ read]
* [ readonly]
* [ Command Substitution]
| style="padding-left:15px;"|Control Flow Statements
* [ Purpose]
* [ test command]
* [,conditions%20that%20we%20may%20set. if statement]
* [ if-else statement]
* ['for%20loop'%20is%20a,files%20using%20a%20for%20loop. for loop]
''<b>User-defined variables</b> are variables which can be '''created by the user''' and exist in the session. This means that no one can access user-defined variables that have been set by another user,<br>and when the session is closed these variables expire.''<br>Reference:
Data can be stored and removed within a variable using an '''equal sign'''.<br><br>The '''read''' command can be used to prompt the user to enter data into a variable.<br>Refer to the diagram on the right-side to see how user-defined variables are assigned data.
'''Positional Parameters and Special Parameters'''
Refer to the diagram to the right for examples using positional and special parameters.
'''Command Substitution:'''
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
<i>'''Command substitution''' is a facility that allows a command<br>to be run and its output to be pasted back on the command line as arguments to another command.</i><br>Reference:<br><br>
<span style="font-family:courier"><b>command1 $(command2)</b><br>or<br><b>command1 `command2`</b></span><br><br>
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br>echo "The current directory is $(pwd)"<br>echo "The current hostname is $(hostname)"<br>echo "The date is: $(date +'%A %B %d, %Y')"<br>
===Using Control Flow Statements in Shell Scripts===
Refer to the diagram immediately to the right for using the '''if logic statement''' with the '''test''' command.
'''if-else statement:'''
[[Image:if-else.png|thumb|right|300px|Example of how an '''if-else''' statement works.<br>(Image licensed under [ cc])]]
Unlike using only an ''if'' statement, an '''if-else''' statement take '''two different sets of actions'''<br>based on the results of the test condition.<br><br>''How it Works:''<br>When the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br>If the test returns a '''FALSE''' value, then the the Linux Commands between<br>the '''else''' and '''fi''' statements are executed.<br><br>
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>else<br>echo &nbsp;&nbsp;&nbsp;“Greater Than or Equal to”<br>fi</span><br><br>
'''Loop Statements'''
# Confirm that you are located in your '''home''' directory in your Matrix account.<br><br>
# Use a text editor to edit the shell script called '''hello'''<br><br>
# Add the following line to the bottom of the file:<br><span style="font-family:courier;">echo "The current shell you are using is: $SHELL(ps -o cmd= -p $$|cut -d' ' -f1)"</span><br><br>'''NOTE:''' This command displays the '''name''' of the ''shell'' that the shell script<br>is running within. The command within '''$( )''' uses a technique known as "''command substitution''"<br>which you will learn about in '''week 12'''.<br><br># '''Save ''' your editing changes and '''exit ''' your text editor.<br><br># Issue the following linux command to change to the '''Bourne Shell (a different shell than the default Bash)''':<br><span style="color:blue;font-weight:bold;font-family:courier;">sh</span><br><br>You should notice your shell prompt change indicating you are in a different shell.<br><br># Issue the following linux command to confirm you are run your shell script in the ''Bourne Shell'':<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $SHELL./hello</span><br><br>You should see the output of the command that you are located currently running the shell script in the ''Bourne Shell'' shell.<br><br># Run your shell script'''NOTE: <span style="color:blue;font-weight:bold;font-family:courier;">./hello</span>''' Due to the fact that shells (and their features) have '''evolved''' over a period of time,<br><br>What an error may occur if you include a ''NEWER'' shell does the feature (e.g. ''Bash Shell'') in your shell script indicate is running?,<br>You should notice that this script is being but run it in the an ''OLDER'' shell (e.g. ''Bourne shellShell'').<br><br>Although your shell script should work, it is recommended You can add a '''special comment''' to force the BEGINNING of the FIRST line of your shell script to run in a <br>'''specificforce''' shell.<br>This helps prevent your shell script encountering errors when it to run in the incorrect shell<br>you want (i.e. syntax not recognized in a specific for example: the Bash shell).<br><br>
# Edit your '''hello''' shell script using a text editor.<br><br>
# Insert the following line at the '''beginning''' of the '''first''' line of your hello file:<br><span style="font-family:courier;">#!/bin/bash</span><br><br>This is referred to as a '''she-bang line'''. It forces the this script to be run in the '''Bash Shell'''.<br>When your Bash Shell script finishes execution, you are returned to your current shell that you are using<br>(which in our case in Matrix, is still the Bash shell).<br><br># '''Save ''' your editing changes and '''exit ''' your text editor.<br><br># While in the Bourne shell, issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>You should notice that the shell name is running in the '''bash''' shell.<br><br># It is a good idea to rename your shell script to include an '''extension ''' to indicate <br>explain that the file this is a '''Bash Shell script ''' file(referred to as a "''portable Bash shell script''"). <br>Issue the following linux command to rename your shell script file:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv hello hello.bash</span><br><br>
# Run your renamed shell script for confirmation by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello.bash</span><br><br>
# Enter the following linux command to return to your Bash shell: <span style="color:blue;font-weight:bold;font-family:courier;">exit</span><br><br># Issue the following Linux command to confirm you have returned to the Bash shell: <span style="color:blue;font-weight:bold;font-family:courier;">echo $SHELL</span><br><br>Let's use some '''ENVIRONMENT variables''' in our Bash Shell script.<br><br>
# Use a text editor to edit the shell script called '''hello.bash'''<br><br>
# Add the following lines to the bottom of the file:<br><span style="font-family:courier;">echo<br>echo "The current directory location is: $PWD"<br>echo "The current user home directory is: $HOME<br>echo</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-1.bash</span><br><br>
# Use a text editor like vi or nano to create the text file called '''for-2.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-2.bash</span>)<br><br>If you are using the nano text editor, refer to notes on text editing in a previous week in the course schedule.<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;">#!/bin/bash<br>clearecho<br>for x<br>do<br>&nbsp;&nbsp;&nbsp;echo $x<br>done<br>echo "blast-off!"<br>echo</span><br><br>
# Save your editing session and exit the text editor (eg. with vi: press '''ESC''', then type ''':wx''' followed by '''ENTER''').<br><br>
# Issue the following linux command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x for-2.bash</span><br><br>
# Run your shell script by issuing: <span style="color:blue;font-weight:bold;font-family:courier;">./for-2.bash 10 9 8 7 6 5 4 3 2 1</span><br><br>How does this differ from the previous shell script?<br><br>You will learn in a couple of weeks more examples of using loop statements.<br><br>Let's run a '''checking-script''' to confirm that both your '''for-1.bash ''' and '''for-2.bash '''<br>Bash shell scripts exist, have execute permissions, and when run, produce <br>the same expected OUTPUT as required in this tutorial's instructions.<br><br>
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week10-check-3 | more</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>Let's create a Bash shell script that contain '''user-created variables'''.<br><br>

Navigation menu