Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

2,727 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: $(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 run this shell script with change to the '''Bourne Shell (i.e. ''':<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 run your shell script in the ''Bourne Shell''):<br><span style="color:blue;font-weight:bold;font-family:courier;">sh ./hello</span><br><br>You should see the output of the command that you are located currently running the shell script in shows '''sh''' (i.e. the '''Bourne Shell''')shell.<br><br>'''NOTE:''' Due to the fact that shells (and their features) have '''evolved''' over a period of time,<br>an error may occur if you include a ''NEWER'' shell feature (e.g. ''Bash Shell'') in your shell script,<br>but run it in an ''OLDER'' shell (e.g. ''Bourne Shell'').<br><br>You can add a '''special comment''' to the BEGINNING of the FIRST line of your shell script to<br>'''force''' it to run in the shell you want (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>

Navigation menu