Changes

Jump to: navigation, search

Tutorial10: Shell Scripting - Part 1

787 bytes added, 03:18, 17 May 2021
no edit summary
=INVESTIGATION 1: CREATING A SHELL SCRIPT=
<span style="color:red;">'''ATTENTION''': Depending on your ULI101 instructor, you may be required to complete this tutorial for '''marks''' in this course.<br>Please refer to your instructor's course notes and lecture notes regarding evaluation for this course.<br><br>The due date for successfully completing this tutorial (i.e. '''tutorial 10''') is by '''Friday by midnight''' next week (i.e. '''Week 10''').<br>If your instructor has NOT assigned marks for completing this tutorial, you can perform it for '''practice'''.</span><br><br> 
In this investigation, you will learn how to create and run a '''Bash Shell script'''.
# Issue the following linux command to '''add'''<br>execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod u+x hello</span><br><br>
# Issue the following to run your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">./hello</span><br><br>Did your shell script run?<br><br><span style="color:red;">'''ATTENTION:''' Students might get FRUSTRATED when performing their '''assignment 3''' when their Bash shell scripts have errors.<br>One major cause is the the OUTPUT of their Bash shell script when run does not '''EXACTLY match''' the required output<br>for the '''correct''' Bash shell script.<br><br>This requires that you CAREFULLY '''read''' the requirements of your Bash shell script and create it to the EXACT specifications</span><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/myscripts~uli101/week10-check-1</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>
::In the next investigation, you will learn to create and run shell scripts that<br >use '''variables''', '''positional''' and '''special parameters'''. You will also learn how to<br><u>add</u> a '''she-bang line''' at the top of a shell script to force it to run in a specified shell.<br><br>Proceed to the next investigation.<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 will be lost.<br>You will in a <u>future</u> tutorial how to set environment variables in '''start-up''' files.<br><br><span style="color:red;">'''ATTENTION:''' Students might get FRUSTRATED when performing their '''assignment 3''' when their Bash shell scripts have errors.<br>One major cause is the the OUTPUT of their Bash shell script when run does not '''EXACTLY match''' the required output<br>for the '''correct''' Bash shell script.<br><br>This requires that you CAREFULLY '''read''' the requirements of your Bash shell script and create it to the EXACT specifications</span>.<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/myscripts~uli101/week10-check-2 | 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>Unlike '''Environment variables''' that are used to set the environment of the shell or shell scripts,<br>'''User-created''' variables are "customized" that the user can set or allow a user to set the variables' values.<br>Let's create a Bash shell script that contain '''user-created variables'''.<br><br>
# Use a text editor to create a Bash shell script called '''user-variables.bash'''<br><br>
# Add the following lines to the beginning of the ''user-variables.bash'' file:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<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>
# Issue the '''chmod''' command to add execute permissions<br>for the user for the '''dog-years.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;">./dog-years.bash</span><br><br>Enter <u>your</u> age to see what happens.<br><br>
# Issue the following to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/myscripts~uli101/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>
:In the next investigation, you will use '''control-flow statements''' to allow your shell scripts<br>to perform differently under different situations.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-1.bash'''<br>(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-1.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>num1=5<br>num2=10<br>if [ $num1 -lt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "num1 is less than num2"<br>fi</span><br><br>
# Save your editing session and exit the text editor<br>(eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>[[Image:if-1.png|thumb|right|200px|Output of a shell script using the '''if''' control-flow statement.]]
# 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 if-1.bash</span><br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-1.bash</span><br><br>Confirm that the output indicates a correct result.<br><br>
# Use a text editor like vi or nano to create the text file called '''if-2.bash'''<br>(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-2.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>read -p "Enter the first number: " num1<br>read -p "Enter the second number: " num2<br>if [ $num1 -gt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is greater than the second number."<br>fi</span><br><br>
# Save your editing session and exit the text editor<br>(eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>[[Image:if-2.png|thumb|right|320px|Output of a shell script using the '''read''' command and the '''if''' control-flow statement.]]
# 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 if-2.bash</span><br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-2.bash</span><br><br>When prompted, make certain that the '''first number'''<br>is <u>greater than</u> the '''second number'''. What happens?<br><br>
# Use a text editor like vi or nano to create the text file called '''if-3.bash'''<br>(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi if-3.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold">#!/bin/bash<br>read -p "Enter the first number: " num1<br>read -p "Enter the second number: " num2<br>if [ $num1 -gt $num2 ]<br>then<br>&nbsp;&nbsp;&nbsp;echo "The first number is greater than the second number."<br>else<br>&nbsp;&nbsp;&nbsp;echo "The first number is less than or equal to the second number."<br>fi</span><br><br>[[Image:if-3.png|thumb|right|330px|Output of a shell script using the '''if-else''' control-flow statement.]]
# Save your editing session and exit the text editor<br>(eg. with vi: press '''ESC''', then type ''':wxx''' 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 if-3.bash</span><br><br>
# Run your shell script by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">./if-3.bash</span><br><br>Try running the script several times with numbers '''different''' and '''equal'''<br>to each other to confirm that the shell script works correctly.<br><br>'''LOOP STATEMENTS''' are a series of steps or sequence of statements executed<br>repeatedly zero or more times satisfying the given condition is satisfied.''<br>Reference: https://www.chegg.com/homework-help/definitions/loop-statement-3<br><br>There are several loops, but we will look at a '''for''' loop using a '''list'''.<br><br>
# Use a text editor like vi or nano to create the text file called '''for-1.bash'''<br>(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-1.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>echo<br>for x in 5 4 3 2 1<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<br>(eg. with vi: press '''ESC''', then type ''':wxx''' followed by '''ENTER''').<br><br>[[Image:for-1.png|thumb|right|125px|Output of a shell script using the '''for''' loop with a '''list'''.]]
# 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-1.bash</span><br><br>
# Run your shell script by issuing:<br><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'''<br>(eg. <span style="color:blue;font-weight:bold;font-family:courier;">vi for-2.bash</span>)<br><br>
# Enter the following lines in your shell script:<br><span style="font-family:courier;font-weight:bold;">#!/bin/bash<br>echo<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<br>(eg. with vi: press '''ESC''', then type ''':wxx''' 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>[[Image:for-2.png|thumb|right|175px|Output of a shell script using the '''for''' loop <u>without</u> a '''list'''.]]
# Run your shell script by issuing:<br><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>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><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/myscripts~uli101/week10-check-4 | 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>
# After you complete the Review Questions sections to get additional practice, then work on your '''online assignment 3''',<br>'''sections 2 and 3''' labelled '''Interactive Shell Environment''' and '''Introduction To Scripting (phone)'''.<br><br>
# Write a Bash shell script that clears the screen and displays the text Hello World on the screen.<br><br>What '''permissions ''' are required to run this Bash shell script?<br><br>What are the different ways methods that you can run this Bash shell script from the command line?<br><br># Write a Bash shell script that clears the screen, prompts the user for their '''full name''' and then prompts the user for their '''age''',<br>then clears the screen again and welcomes the user by their name and tells them their age.<br><br>What '''comments ''' would you add to the above script’s contents to properly document this Bash shell script to be understood<br>for those users that would read / edit this Bash shell script’s contents?<br><br># Write a Bash shell script that will first set the value of a read-only variable called '''numberdogFactor''' to '''23''' and make this variable '''read-only7'''.<br>Then the The script will then clear the screen and prompt the user to enter a value for that variable called number to another value.<br>Have the script display the value age of the a dog in human years (which will be stored into a variable called number to prove that it is a read-only variable'''humanYears''').<br><br>When you ran this Bash shell The script, did you encounter an error message?will store in a variable called '''dogYears''' the value of ''humanYears x dogFactor''<br>How would you run this Bash shell The script, so will then clear the screen a second time and then display the age of the error message was NOT displayed?dog in ''“dog years”''.<br><br># Write a Bash shell script that will clear the screen and then display all '''arguments ''' that were entered <u>after </u> your Bash shell script when it was run. Also have the Bash shell script display the '''number of arguments ''' that were entered after your Bash shell script.<br><br><br>
'''PART B: WALK-THRUS'''
</pre>
:WRITE ROUGH WORK AND OUTPUT FROM ISSUING:
:'''./walkthru1.bash'''
 
:ROUGH WORK:
 
:OUTPUT:
 
</pre>
:WRITE ROUGH WORK AND OUTPUT FROM ISSUING:
:'''./walkthru2.bash apple orange banana'''
 
:ROUGH WORK:
 
:OUTPUT:
 
<br><br>
:'''Walkthru #3:'''
 
:'''cat walkthru2.bash'''
<pre>
#!/usr/bin/bash
 
for x in 1 2 3 4 5
do
 
if [ $((x % 2)) -eq 0 ]
then
echo "this"
else
echo "that"
fi
 
done
 
</pre>
 
:WRITE ROUGH WORK AND OUTPUT FROM ISSUING:
:'''./walkthru3.bash apple orange banana'''
 
:ROUGH WORK:
 
:OUTPUT:
[[Category:ULI101]]
13,420
edits

Navigation menu