Open main menu

CDOT Wiki β

Changes

Tutorial11: Shell Scripting - Part 1

463 bytes added, 20:36, 4 September 2023
no edit summary
{{Admon/caution|DO NOT USE THIS VERSION OF THE LAB. This page will no longer be updated.|'''New version here:''' https://seneca-ictoer.github.io/ULI101/A-Tutorials/tutorial11<br />'''Andrew's students please go here:''' http://wiki.littlesvr.ca/wiki/OPS145_Lab_10}}
=INTRODUCTION TO SHELL SCRIPTING=
<br>
|- valign="top" style="padding-left:15px;"
|colspan="2" |'''Slides:'''<ul><li>Week 10 11 Lecture 1 Notes:<br> [https://matrixwiki.cdot.senecacollege.ca/~chris.johnsonuli101/ULI101slides/ULI101-Week1011.1.pdf | PDF] | [https://matrixwiki.cdot.senecacollege.ca/~chris.johnsonuli101/ULI101slides/ULI101-Week1011.1.pptx PPTX]<br>Week 10 11 Lecture 2 Notes:</li><li> [https://matrixwiki.cdot.senecacollege.ca/~chris.johnsonuli101/ULI101slides/ULI101-Week1011.2.pdf | PDF]] | [https://matrixwiki.cdot.senecacollege.ca/~jason.carmanuli101/slides/ULI101-Week1011.2.pptx PPTX] <br></li></ul>
=INVESTIGATION 1: CREATING A SHELL SCRIPT=
<span style="color:red;">'''ATTENTION''': Effective '''May 9, 2022''' - this This online tutorial will be required to be completed by '''Friday in week 12 by midnight'''<br>to obtain a grade of '''2%''' towards this course</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>
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week10week11-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>
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week10week11-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 '''dog-years.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;">~uli101/week10week11-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>
# 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;">~uli101/week10week11-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>
= LINUX PRACTICE QUESTIONS =
simulate a quiz:
https://ictwiki.cdot.senecacollege.ca/~murray.saululi101/uli101files/uli101_week10_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
:OUTPUT:
 
 
 
_________________________________________________________________________________
 
Author: Murray Saul
 
License: LGPL version 3
Link: https://www.gnu.org/licenses/lgpl.html
 
_________________________________________________________________________________
 
 
 
[[Category:ULI101]]