Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

852 bytes added, 10:02, 26 October 2020
Creating & Executing Shell Scripts
===Main Objectives of this Practice Tutorial===
:* Understand Explain the purpose of the process for '''planningshe-bang line''' prior to writing contained at the top of a shell script.
:* Understand the purpose of List rules for naming a '''she-bang line''' contained at the top of a shell Bash scriptfile.
:* Setting Explain how to set '''permissions''' for a shell script and properly how to '''execute''' a shell script.
:* Understand and use Explain the purpose of '''environment''' and '''user-defined''''''Bold text''' variables within a shell script.
:* Understand Explain the purpose of '''control flow statements''' used with shell scripts.
:* Use Explain the purpose of the '''test''' command to test various conditions.
:* Use Explain the purpose and usage of the '''if''' logic statement and the '''for''' loop statement within shell scripts.
===Tutorial Reference Material===
| style="padding-left:15px;" |Shell Scripting
* [https://searchdatacenter.techtarget.com/definition/shell-script Purpose]
* [https://www.youtube.com/watch?v=cQepf9fY6cE Creating and Running a Shell Script]<br>
Variables
* [https://opensource.com/article/19/8/what-are-environment-variables Environment]
* [https://www.cyberciti.biz/faq/bash-for-loop/#:~:text=A%20'for%20loop'%20is%20a,files%20using%20a%20for%20loop. for loop]
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=kxEP-KUhOSg&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=5 Introduction to Bash Shell Scripting- Part 1]</li><li>[https://www.youtube.com/watch?v=XVTwbINXnk4&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=6 Using Variables cQepf9fY6cE Creating and Control Flow Statements in Running a Shell ScriptingScript]</li></ul>
|}
First, list the INPUTS into the script (eg. prompting user for data, reading data from file, etc), then listing the expected OUTPUTS from the script. You can then list the steps to process the INPUT to provide the OUTPUT (including file storage).
Once you have planned your shell script by listing the sequence of steps (i.e. PROCESSING) in your script, you need to create a file (using a '''text editor''') that will contain your Linux commands.<br><br>'''NOTE:''' Avoid using filenames of already existing Linux Commands to avoid confusion. <br>Using shell script filenames that include the file extension of the shell that the script will run within is recommended.
'''Using a Shebang Line'''
[[Image:shebang.png|thumb|right|200px|The '''shebang line''' <u>must</u> appear on the '''first line''' and at the '''beginning''' of the shell script.]]If you are learning Bash scripting by reading other people’s code you might have noticed<br>that the first line in the scripts starts with the <span style="font-family:courier;font-weight:bold">#!</span> characters and the path to the Bash interpreter. <i>This sequence of characters (#!) is called '''shebang''' and is used to tell the operating system<br>which interpreter to use to parse the rest of the file. </i>Reference: https://linuxize.com/post/bash-shebang/
The '''shebang line''' <u>must</u> appear on the '''first line''' and at the '''beginning''' of the shell script,<br>otherwise, it will be treated as a regular comment and ignored.
# 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>
# Issue the following linux command to confirm you are in the Bourne Shell:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $SHELL</span><br><br>You should see the output of the command that you are located in the Bourne Shell.<br><br>
# Re-run Run your shell script: <span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>What shell does the shell script indicate is running?<br>You should notice that this script is being run in the Bourne shell.<br><br>Although your shell script should work, it is recommended to force your shell script to run in a specific shell. This helps prevent your shell script encountering errors when run in the incorrect shell (i.e. syntax not recognized in a specific 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 script to be run in the Bash Shell. When your Bash Shell script finishes execution, although the you are returned to your current shell that you are using (which in our case in Matrix, is still the Bourne Bash shell).<br><br># Save your editing changes and exit your text editor.<br><br>
# It is a good idea to rename your shell script to include an extension to indicate that the file is a Bash Shell script file. <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 "The current username is: $USER"<br>echo "The current directory location is: $PWD"<br>echo "The current user's home directory is: $HOME</span><br><br>
# Save your editing changes and exit your text editor.<br><br>
# Run your '''renamed shell script for confirmation by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello.bash''' shell script.</span><br><br>Take time to view the output and the values of the environment variables.<br><br># Issue the following linux command to add your current directory to the '''PATH ''' environment variable:<br><span style="color:blue;font-weight:bold;font-family:courier;">PATH=$PATH:.</span><br><br>
# Issue the following linux command to confirm that the current directory "." has been added to the PATH environment variable:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $PATH</span><br><br>
# Run the '''hello.bash''' by just shell script name (i.e. to not use ./ prior to shell script name).<br><br>The shell script should run just by name.<br><br>
# Exit your Matrix session, and log back into your Matrix session.<br><br>
# Re-run the '''hello.bash''' shell script by just using the name.<br><br>What did you notice?<br><br>The setting of the '''PATH ''' environment variable only worked in the current session only. <br>If you exit the current Matrix session, then the recently changed settings for environment variables are will be lost. <br>You will learned in Week 12 a future tutorial how to set environment variables in startup files.<br><br>Let's create a Bash shell script that contain '''user-created variables'''.<br><br>
# Use a text editor to create a file called '''user-variables.bash'''<br><br>
# Add the following lines to the bottom beginning of the this file:<br><span style="font-family:courier;">#!/bin/bash<br>age=25<br>readonly age<br>read -p "Enter your Full Name" name<br>read -p "Enter your age (in years): " age<br>echo "Hello $name - You are $age years old"</span><br><br>
# Save your editing changes and exit your text editor.<br><br>
# Issue the '''chmod''' command to add execute permissions for the user for the '''user-variables.bash''' file.<br><br>
# Issue the following to run the user-variables.bash Bash shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">./user-variables.bash</span><br><br>What do you notice when you try to change the age variable? Why?<br><br>
# Use a text editor to create a file called '''parameters.bash'''<br><br>
# Add the following lines to the bottom beginning of the this file:<br><span style="font-family:courier;">#!/bin/bash<br>echo \$0: $0<br>echo \$2: $2<br>echo \$3: $3<br><br>echo \$#: $#<br>echo \$*: $*<br><br>shift 2<br>echo \$#: $#<br>echo \$*: $*</span><br><br>
# Save your editing changes and exit your text editor.<br><br>
# Issue the '''chmod''' command to add execute permissions for the user for the '''parameters.bash''' file.<br><br>
'''Perform the Following Steps:'''
 : Before learning about logic and loop control-flow statements, you need to first learn about issuing test conditions using the '''test''' command. <br><br># Issue the following linux commands at the Bash shell prompt to assign values to several variables:<br><span style="color:blue;font-weight:bold;font-family:courier;">course="ULI101"<br>number1=5<br>number2=10</span><br><br># Issue the following linux command to test a condition:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $course = "ULI101"</span><br><br>The '''$?''' variable is used to store an exit status of the previously command issued (including the test command). If the status is '''zero''', then it indicates a TRUE value and if the status is '''non-zero''', then it indicates a FALSE value.<br><br>
# Issue the following linux command to view the status of the previously-issued '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">echo $?</span><br><br>Based on its value, is the result TRUE or FALSE?<br><br>
# Issue the following linux command to test another condition:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $course = "uli101"</span><br><br>
# Issue the following linux command to test a condition involving numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $number1 > $number2</span><br><br>
# Issue a linux command to display the value of '''$?'''. '''NOTE:''' You will notice that something is '''wrong'''.<br>The exit status '''$?''' shows a zero (TRUE) value, but the number 5 is definitely NOT greater than 10.<br>The problem is that the symbols '''&lt;''' and '''&gt;''' are interpreted as REDIRECTION symbols!<br><br>
# To prove this, issue the following linux command :<br><span style="color:blue;font-weight:bold;font-family:courier;">ls 10</span><br><br>You should notice a file called "'''10'''". The incorrectly issued '''test''' command used redirect to create an empty file instead,<br> which indeed succeeded just giving a TRUE value!<br><br>To prevent these incorrectly issued testing for number comparisonproblems when issuing the '''test''' command when comparing numbers, you can use the following options instead:<br>'''-lt''' (&lt;), '''-le''' (&lt;&#61;), '''-gt''' (&gt;), '''-ge''' (&gt;&#61;;), '''-eq''' (&#61;), '''-ne''' (!&#61;)<br><br>
# Issue the correct linux command to properly test both values:<br><span style="color:blue;font-weight:bold;font-family:courier;">test $number1 -gt $number2</span><br><br>
# Issue a linux command to display the value of '''$?'''. <br>You should notice that the exit status value is now FALSE which is the correct result.<br><br># The '''test''' command can be abbreviated by the square brackets '''&#91; &#93; ''' which contain the test condition within the square brackets. You need to have spaces between the brackets and the test condition; otherwise, you will get a test error.<br><br># To generate a '''test error''', issue the improper use of copy and paste the following '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">&#91;$number1 -gt $number2&#93;</span><br><br>You should notice an test error message.<br><br># Issue Copy and paste the following (correct use of the ) '''test''' command:<br><span style="color:blue;font-weight:bold;font-family:courier;">&#91; $number1 -gt $number2 &#93;</span><br><br>Issue a command to view the value of the exit status of the previously issued '''test''' command. You should notice that is works properly.<br><br>'''Let's now learn about control-flow statements:'''<br><br>'''Logic statements''' are used to create different paths or directions that the script can execute based in on the result of testing conditions. In this tutorial, we will only focus on the '''if''' logic statement.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-1.bash''' (eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-1.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>clear<br>num1=5<br>num2=10<br>if [ $num1 -gt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "Greater Than"<br>fi</span><br><br>
13,420
edits

Navigation menu