13,420
edits
Changes
no edit summary
=INTRODUCTION TO SHELL SCRIPTINGUSING SED & AWK UTILTIES=
<br>
===Main Objectives of this Practice Tutorial===
:* Plan and create Use the '''sed''' command to '''manipulate text''' contained in a Shell Scriptfile.
:* Explain the purpose of List and explain several '''addresses''' and '''instructions''' associated with the '''she-bang linesed''' contained at the top of a shell scriptcommand.
:* Set Use the '''permissionssed''' and command as a '''executefilter''' a shell scriptwith Linux pipeline commands.
:* Use the '''environmentawk''' and command to '''user-definedmanipulate text''' variables contained in shell scriptsa file.
:* Use List and explain '''comparison operators''Command Substitution', '''variables''' and '''Math Operationsactions''' associated with the '''awk''' in shell scriptscommand.
:* Explain the purpose of the '''$?''' exit status and Use the '''testawk''' command. :* Use '''if''' and '''if-else''' logic statements in shell scripts. :* Use as a '''forfilter''' loop statement with a list in shell scriptsLinux pipeline commands.
<br><br>
|- valign="top" style="padding-left:15px;"
|colspan="2" |Course Notes'''Slides''':<ul><li>Week 11 Lecture 1 Notes:<br> [[Media:ULI101-Week11.1.pdf | PDF]] | [https://ictmatrix.senecacollege.ca/~murraychris.sauljohnson/uli101ULI101/ULI101-Week11.1.pptx PPTX]</li><li>Week 11 Lecture 2 Notes:<br> [[Media:ULI101-Week10Week11.2.pdf | PDF]] | [https://ictmatrix.senecacollege.ca/~murrayjason.saulcarman/uli101slides/ULI101-Week10Week11.2.pptx PPTX]<br></li></ul>
| style="padding-left:15px;" |'''Text Manipulation:'''
* [https://www.digitalocean.com/community/tutorials/the-basics-of-using-the-sed-stream-editor-to-manipulate-text-in-linux Purpose of using the sed utility]
* [https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-linux Purpose of using the awk utility]
| style="padding-left:15px;" |Shell Scripting'''Commands:'''* [https://searchdatacenterman7.org/linux/man-pages/man1/sed.techtarget1p.comhtml sed]* [https:/definition/shellman7.org/linux/man-script Purposepages/man1/awk.1p.html awk]
|colspan="1" style="padding-left:15px;" width="30%"|'''Brauer Instructional Videos:'''<ul><li>[https://www.youtube.com/watch?v=kxEP-KUhOSgnpU6S61AIko&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=5 Bash Shell Scripting - Part 114 Using the sed Utility]</li><li>[https://www.youtube.com/watch?v=cQepf9fY6cE Creating and Running a Shell ScriptOV3XzjDYgJo&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=13 Using the awk Utility]</li></ul>
|}
= KEY CONCEPTS =
'''Using a Shebang LineHow it Works:'''
* Can use a line number, to select a specific line (for example: '''Definition5''')* Can specify a range of line numbers (for example: '''5,7''')* Regular expressions are contained within forward slashes (e.g. /regular-expression/)* Can specify a regular expression to select all lines that match a pattern (e.g '''/^[0-9].*[0-9]$/''') * If NO address is present, the instruction will apply to ALL lines
<br><br>
* The '''Logic Statementsawk''' command reads all lines in the input file and will be exposed to the expression (contained within quotes) for processing.*The '''expression''' (contained in quotes) represents '''selection criteria''', and '''action''' to execute contained within braces '''{}'''* if selection criteria is matched, then action (between braces) is executed.* The '''–F''' option can be used to specify the default '''field delimiter''' (separator) character<br>eg. '''awk –F”;”''' (would indicate a semi-colon delimited input file).<br>'''Selection Criteria'''
<br>
=INVESTIGATION 1: USING THE SED UTILITY=
<span style=INVESTIGATION 1"color: CREATING A SHELL SCRIPT=red;">'''ATTENTION''': Effective '''May 9, 2022''' - this online tutorial will be required to be completed by '''Friday in week 11 by midnight'''<br>to obtain a grade of '''2%''' towards this course</span><br><br>
'''Perform the Following Steps:'''
# '''Login''' to your matrix accountand confirm you are located in your '''home''' directory.<br><br># Issue a Linux command to create a directory called '''sed'confirm''<br><br># Issue a Linux command to <u>change</u> to the '''sed''' directory and confirm that you are located in your the '''homesed''' directory.<br><br>We want to create a Bash Shell script # Issue the following Linux command to welcome download the user by their data.txt file<br>('''copy and paste'username''.to save time):<br>Let's first provide some useful tips in terms of selecting an appropriate name for the shell script<span style="color:blue;font-weight:bold;font-family:courier;">wget <nowiki>https://ict.senecacollege.ca/~murray.saul/uli101/data.txt</nowiki></span><br><br># Issue the following linux '''more''' command to check if your intended shell script namequickly view the contents of the '''data.txt''' file.<br>is already exists to be run automatically from When finished, exit the more command by pressing the Bash shell:<br>letter <span style="color:blue;font-weight:bold;font-family:courier;">which helloq</span>[[Image:sed-1.png|thumb|right|300px|Issuing the '''p''' instruction without using the '''-n''' option (to suppress original output) will display lines twice.]]<br><br>You should notice that there The '''p''' instruction with the '''sed''' command is no output and therefore, this shell script name CAN be used.to<br><br># Use a '''text editorprint''' like (i.e. ''display'vi') the contents of a text file.<br><br># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed 'p' or data.txt</span><br><br>'''nanoNOTE: You should notice that each line appears twice''' to create .<br><br>The reason why standard output appears twice is that the sed command<br>(without the text file called '''hello-n option''') displays all lines regardless of an address used.<br><br>If you are using We will use '''pipeline commands''' to both display stdout to the nano text editor, refer screen and save to notes on text editing in files<br>for <u>confirmation</u> of running these pipeline commands when run a previous week '''checking-script''' later in the course schedulethis investigation.<br><br># Enter Issue the following two lines in your shell script, replacing "your-username" with your actual nameLinux pipeline command:<br><span style="color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">echosed -n 'p' data.txt | tee sed-1.txt<br/span>echo "Hello $USER"<br>echo</spanbr>What do you notice? You should see only one line.<br><br>You can specify an '''NOTE:address''' The variable called to display lines using the sed utility<br>(eg. ''line #'USER', '' is an 'line #s''environment'or range of '' variable that contains the current user'line #s login name''').<Brbr>In this way if you wanted to share your shell script with other users, when they run <br># Issue the shell scriptfollowing Linux pipeline command:<br>it will greet by <uspan style="color:blue;font-weight:bold;font-family:courier;">theirsed -n '1 p' data.txt | tee sed-2.txt</uspan><br><br>You should see the first line of the text file displayed.<br>What other command is used to only display the first line in a file?<br><br> username[[Image:sed-2. png|thumb|right|500px|Using the sed command to display a ''environment variables'range' makes your shell script to be more '' of lines.]]# Issue the following Linux pipeline command:<br><span style="usablecolor:blue;font-weight:bold;font-family:courier;" by others>sed -n '2,5 p' data.txt | tee sed-3.txt</span><br><br># Save your editing session and exit What is displayed? How would you modify the sed command to display the text editor.line range 10 to 50?<br><br>Instead of issuing the The '''s''bash'instruction is used to '' command followed by your shell script pathname as an 'substitute'argument'',text<br>let's simply run it by its filename. This is (a similar to method was demonstrated in the most common method of running shell scriptsvi editor in tutorial 9).<br><br># Issue the following linux Linux pipeline command to run your shell script in your current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed '2,5 s/TUTORIAL/LESSON/g' data./hellotxt | tee sed-4.txt | more</span><br><br>'''You should What do you notice an error indicating you don't have permissions ? View the original contents of lines 2 to run 5 in the file'''data.txt''' file<br>You need in another shell to first add confirm that the substitution occurred.<br><br>[[Image:sed-3.png|thumb|right|500px|Using the sed command with the '''execute permissions-q''' prior option to display up to running a line number, then quit.]]The '''q''' instruction terminates or '''quits''' the execution of the shell scriptsed utility as soon as it is read in a particular line or matching pattern.<br><br># Issue the following linux Linux pipeline command to add execute permissions for your shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">chmod sed '11 q' data.txt | tee sed-5.txt</span><br><br>What did you notice? How many lines were displayed<br>before the sed command exited?<br><br>You can use '''regular expressions''' to select lines that match a pattern. In fact,<br>the sed command was one of the <u+x hello>first</u> Linux commands that used regular expression.<br><br>The rules remain the same for using regular expressions as demonstrated in '''tutorial 9'''<br>except the regular expression must be contained within '''forward slashes'''<br>(eg. <spanstyle="font-family:courier;font-weight:bold;">/regexp/</span> ).<br><br>[[Image:sed-4.png|thumb|right|400px|Using the sed command using regular expressions with '''anchors'''.]]# Re-run your shell scriptIssue the following Linux pipeline command: <br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '/^The/ p' data./hellotxt | tee sed-6.txt</span><br><br>Did What do you shell script runnotice?<br><br># Issue the following Linux pipeline command:<br><span style="color:redblue;font-weight:bold;font-family:courier;">sed -n '/d$/ p'data.txt | tee sed-7.txt</span><br><br>What do you notice?<br><br>The 'ATTENTION:''sed''' Students will get FRUSTRATED when performing their utility can also be used as a '''assignment 3filter''' when their Bash shell scripts have errorsto manipulate text that<br>was generated from Linux commands.<br><br>[[Image:sed-5. One major cause is png|thumb|right|400px|Using the the OUTPUT of their Bash shell script when run does not sed command with '''EXACTLY matchpipeline''' commands.]]# Issue the required output for the correct Bash shell scriptfollowing Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">who | sed -n '/^[a-m]/ p' | tee sed-8. This requires that txt | more</span><br><br>What did you CAREFULLY notice?<br><br># Issue the following Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls | sed -n '/txt$/ p''read''' the requirements of your Bash shell script and create it to the EXACT specifications| tee sed-9.txt</span><br><br>What did you notice?<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~uli101/murray.saul/scripts/week10week11-check-1</span><br><br># If you encounter errors, make corrections and '''re-run''' the checking script until you<br>until you 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> =INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS =
:In this sectionthe next investigation, you will learn how to use variables, positional and special parameters to assist you in creating adaptable shell scriptsmanipulate text using the '''awk''' utility.<br><br>
=INVESTIGATION 2: USING THE AWK UTILITY =
In this investigation, you will learn how to use the awk utility to manipulate text and generate reports.
'''Perform the Following Steps:'''
# Confirm that Change to your '''home''' directory and issue a command to '''confirm'''<br>you are located in your '''home''' directory in your Matrix account.<br><br># Use Issue a text editor Linux command to edit the shell script create a directory called '''helloawk'''<br><br># Add the following line Issue a Linux command to the <u>bottomchange</u> of to the file:<br><span style="font-family:courier;font-weight:bold;">echo "The current shell '''awk''' directory and confirm you are using is: $(ps -o cmd= -p $$|cut -dlocated in the '''awk'' ' -f1)"directory.<br></spanbr>Let's download a database file that contains information regarding classic cars.<br><br>'''NOTE:''' This # Issue the following linux command displays the ('''namecopy and paste''' of the ''shell'' that the shell script is running in.to save time):<br>The command within <span style="color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">$( )wget <nowiki>https://ict.senecacollege.ca/~murray.saul/uli101/cars.txt</nowiki></span> uses a technique known as '''command substitution'''.<<br><br># Issue the following linux '''cat''' command to change to quickly view the contents of the '''Bourne Shellcars.txt''':file.<br><span style=br>The "color:blue;font-weight:bold;font-family:courier;'''print'''"action (command) is the <u>shdefault</spanu>action of awk to print<br><br>You should notice your shell prompt changed all selected lines that indicates that you are in match a different shell'''pattern'''.<br><br># Issue the following linux command to run your shell script This '''action''' (contained in the braces) can provide more options<br>such as printing '''specific fields'Bourne Shell'':of selected lines (or records) from a database.<br><span style="color:blue;fontbr>[[Image:awk-weight:bold;font-family:courier;">1./hello</span><br><br>You should see that you are currently running the shell script "'''sh'''" that represents png|thumb|right|400px|Using the awk command to display matches of the pattern '''Bourne shellford'''.<br><br>]]# Issue the following linux command all to display all lines (i.e. records) in the '''NOTE:cars.txt''' Due to database that matches the fact that shells pattern (and their featuresor "make") have called '''evolvedford''' over a period of time,:<br>an error may occur if you include a ''NEWER<span style="color:blue;font-weight:bold;font-family:courier;">awk '/ford/ {print}' shell feature (ecars.g. ''Bash Shell'') in your shell script,txt</span><br><br>but run it in an ''OLDERWe will use '' shell (e.g. 'pipeline commands'Bourne Shell'').to both display stdout to the screen and save to files for <bru>confirmation<br/u>You can add of running these pipeline commands when run a '''special commentchecking-script''' to the BEGINNING of the later in this investigation.<ubr>FIRST line</ubr> of your shell script # Issue the following linux pipeline command all todisplay records<br>in the '''forcecars.txt''' it to run in database that contain the shell you want pattern (for example: the Bash shelli.e. make).<br><br> # Edit your '''helloford''' shell script using a text editor.:<br><br># '''Insert''' the following line at the '''beginning''' of the '''first''' line of your hello filespan style="color:blue;font-weight:<br><span style="bold;font-family:courier;font-weight:bold;">#!awk '/binford/bash' cars.txt | tee awk-1.txt</span><br><br>This is referred to as a '''she-bang line'''. It forces this script to be run in the What do you notice? You should notice ALL lines displayed <u>without</u> using '''Bash Shellsearch criteria'''.<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># <br>You can use ''builtin''Save''' your editing changes and variables'''exitwith the '''print''' your text editorcommand for further processing.<br><br># While in the Bourne shell, issue We will discuss the following linux commandvariables in this tutorial:<br><span style="colorbr>[[Image:blue;font-weight:bold;font-family:courier;">awk-2./hello</span><br><br>You should notice that the shell name is running in the png|thumb|right|400px|Using the awk command to print search results by '''field number'''.]]'''Bash shell$0'''.- Current record (entire line)<br><'''$1''' - First field in record<br> # It is a good idea to rename your shell script to include an '''extension$n''' to- nth field in record<br>explain that this is '''Bash Shell scriptNR''' file - Record Number (referred to as a "order in database)<br> '''NF'portable Bash shell script''")- Number of fields in current record<br><br>For a listing of more variables, please consult your course notes. <br><br># Issue the following linux pipeline command to rename your shell script file:display the '''model''', '''year''', '''quantity''' and price<br><span style=in the '''cars.txt''' database for makes of '''chevy''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mv hello hello.bash<awk '/chevy/span><br{print $2,$3,$4,$5}' cars.txt | tee awk-2.txt</span><br># Run your renamed shell script for confirmation by issuing:<br><span style="color:blue;font-weight:bold;font-family:courier;">Notice that a '''space''' is the delimiter for the fields that appear as standard output./hello.bash</span><br><br># Enter the following linux command to The '''exit'tilde character'' the ''Bourne shell'' and return to your ~''Bash shell'':is used to search for a pattern or display standard output for a particular field.<br><span style="color:blue;font-weight:bold;font-family:courier;">exit</span><br><br>Let# Issue the following linux pipeline command to display all 's use some ''plymouths'ENVIRONMENT variables''' in our Bash Shell script.<br><br># Use a text editor to edit the shell script called ('''hello.bashplym''')<br>by '''model name''', '''price''' and '''quantity''':<br># Add the following lines to the <u>bottom</u> of the file:<br><span stylespan style="color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">echo<brawk '$1 ~ /plym/ {print $2,$3,$4,$5}' cars.txt | tee awk-3.txt</span>echo "The current directory location is: $PWD"<br>echo "The current user home directory is: $HOME"<<br>echo</span>You can also use '''comparison operators''' to specify conditions for processing with matched patterns<br><br># Save your editing changes and exit your text editorwhen using the awk command.Since they are used WITHIN the awk expression,<br>they are not confused with redirection symbols<br># Run your renamed shell script for confirmation by issuing:<br><span style="color[[Image:blue;fontawk-weight:bold3.png|thumb|right|400px|Using the awk command to display results based on '''comparison operators'''.]]'''<''' font-family:courier ">./hello.bash Less than</spanbr>'''<=''' Less than or equal<br>'''>''' Greater than<br>Take time to view the output and the values of the environment variables.<br'''>=''' Greater than or equal<br># Issue the following linux command to add your current directory to the '''PATH==''' environment variable: Equal<br><span style'''!="color:blue''' font-weight:bold font-family:courier ">PATH=$PATH:.</span><brNot equal<br><br># Issue the following linux pipeline command to confirm that display display the current directory '''"."car make''', '''model'' has been added to the PATH environment variable:<br', '''quantity''' and '''price''' of all vehicles whose '''prices are less than $5,000''':<br><span style="color:blue;font-weight:bold;font-family:courier;">echo awk '$PATH5 < 5000 {print $1,$2,$4,$5}' cars.txt | tee awk-4.txt</span><br><br>What do you notice?<br><br># Issue the following linux pipeline command to run your Bash shell script just by name:display display '''price''',<br><span style="color:blue;font-weight'''quantity''', '''model''' and '''car make''' of vehicles whose '''prices are less than $5,000''':bold;font<br><span style="color:blue;font-weight:bold;font-family:courier;">hello.bashawk '$5 </span><br>5000 {print $5,$4,$2,$1}' cars.txt | tee awk-5.txt<br/span>Did your Bash shell script run?<br><br># Exit your Matrix session, and log back into your Matrix session.<br>Issue the following linux pipeline command to display the '''car make''',<br># Re-run the '''hello.bashyear''' and '''quantity''' shell script by just using the name.<br><br>What did you notice?<br><br>The setting of cars that '''begin''' with the '''PATHletter 'f'''' environment variable only worked in the current session only.:<br>If you exit the current Matrix session<span style="color:blue;font-weight:bold;font-family:courier;">awk '$1 ~ /^f/ {print $1, then the recently changed settings for environment variables will be lost$2,$4}' cars.txt | tee awk-6.txt<br/span>You will in a <ubr>future</ubr> tutorial how to set environment variables in [[Image:awk-4.png|thumb|right|400px|Using the awk command to display combined search results based on '''start-upcompound operators''' files.]]Combined pattern searches can be made<br><br><span style="color:red;">by using '''ATTENTION:compound operator''' Students will get FRUSTRATED when performing their symbols:<br><br>'''assignment 3&&''' when their Bash shell scripts have errors. One major cause is the the OUTPUT of their Bash shell script when run does not (and)<br>'''EXACTLY match||''' (or)<br><br># Issue the required output for the correct Bash shell script. This requires that you CAREFULLY following linux pipeline command to list all '''fords'''<br>whose '''readprice is greater than $10,000''' 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 stylespan style="color:blue;font-weight:bold;font-family:courier;">bash awk '$1 ~ /homeford/murray&& $5 > 10000 {print $0}' cars.saul/scripts/week10txt | tee awk-check-2 | more7.txt</span><br><br>If you encounter errors, make corrections and # Issue the following linux command ('''re-runcopy and paste''' the checking script until you<br>receive a congratulations message, then you can proceed.to save time):<br><br>Let's create a Bash shell script that contain '''userspan style="color:blue;font-weight:bold;font-created variables'''.<brfamily:courier;">wget <brnowiki># Use a text editor to create a Bash shell script called '''user-variableshttps://ict.senecacollege.ca/~murray.saul/uli101/cars2.bash'''txt</nowiki></span><br><br># Add Issue the following lines '''cat''' command to quickly view the beginning contents of this the '''cars2.txt''' file:.<br><span style="font-family:courier;font-weight:bold;">#!/bin/bashbr># Issue the following linux pipeline command to display the '''year'''<br>read -p "Enter your Full Name: " name<br>read -p "Enter your age (in years): " age<and '''quantity''' of cars that '''begin''' with the '''letter 'f'''' for the '''cars2.txt''' database:<br>echo "Hello $name - You are $age years old"</spanstyle="color:blue;font-weight:bold;font-family:courier;"><br><br># Save your editing changes and exit your text editorawk '$1 ~ /^f/ {print $2,$4}' cars2.txt | tee awk-8.txt</span><br><br>What did you notice?<br><br># Issue The problem is that the '''chmodcars2.txt''' command to add database separates each field by a semi-colon ('''execute permissions;''' for the user for the ) <u>instead</u> of '''user-variables.bashTAB''' file.<br>Therefore, it does not recognize the second and fourth fields.<br># Issue <br>You need to issue awk with the following -F option to run the userindicate that this file's fields are separated (delimited) by a semi-variablescolorn.bash Bash shell script:<br><span style="color:blue;font-weight:bold;font-family:courier;">./user-variables.bash</span><br><br>What did you notice?<br><br># Use a text editor to # Issue the following linux pipeline command to display the '''year'''<br>and '''modifyquantity''' your Bash shell script called of cars that '''begin''user-variables.bash'with the ''<br><br># 'letter 'f'Insert''' for the following lines immediately <u>'''belowcars2.txt'''database:</ubr> the '''she-bang''' line:<br><<span style="color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">age=25<br>readonly ageawk -F";" '$1 ~ /^f/ {print $2,$4}' cars2.txt | tee awk-9.txt</span><br><br># Save your editing changes and exit your text editor.What did you notice this time?<br><br># Issue the following to run the user-variables.bash Bash shell a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">.~uli101/userweek11-check-variables.bash2</span><br><br>What do If you notice when you try to change the age variable? Why?<br><br>Letencounter errors, make corrections and '''re-run's use '''positional parameters''' and '''special parameters''' in a Bash shell the checking script.until you<br><br># Use a text editor to create a file called '''parameters.bash'''<br><br># Add the following lines to the beginning of this file:<br><span style="font-family:courier;font-weight:bold;">#!/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>Notice how the quoting character "'''\'''" is used to display positional parameters like "'''$2'''"<br>as opposed to the value stored in the <u>second</u> positional parameter.<br><br># Issue the '''chmod''' command to add '''execute permissions''' for the user for the '''parameters.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;">./parameters.bash</span><br><br>What happened?<br><br>The values for some of the ''positional parameters'' and ''special parameters'' may NOT be<br>displayed properly since you did NOT provide any '''arguments''' when <u>running</u> your Bash shell script.<br><br># Issue the following to run the user-variables.bash Bash shell script with arguments:<br><span style="color:blue;font-weight:bold;font-family:courier;">./parametersreceive a congratulations message, then you can proceed.bash 1 2 3 4 5 6 7 8</span><br><br>What do you notice?<br><br>Take some time to view the results and how the ''parameters'' have changed when using the '''shift''' command. <br>
= LINUX PRACTICE QUESTIONS =
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).
'''Review Questions:'''
Note the contents from the following tab-delimited file called ''For example:'~murray.saul/uli101/stuff.txt''':(this file pathname exists for checking your work)
<span style="font-family:courier;font-weight:bold;"pre>echo "The current date Line one.This is the second line.This is the third.This is line four.Five.Line six followsFollowed by 7Now line 8and time is: $(date)"line nineFinally, line 10</spanpre>
# Confirm that you are located in your <span style="font-family:courier;font-weight:bold">sed -n '3,6 p''home''' directory in your Matrix account~murray.saul/uli101/stuff.txt<br/span><br># Use a text editor to create a Bash shell script called '''command-substitution.bash'''<br><br># Add the following lines to the beginning of this file:<br><span style="font-family:courier;font-weight:bold;">#!sed '4 q' ~murray.saul/binuli101/bashstuff.txt<br/span>echo<br>echo "MY ACCOUNT INFORMATION:"<br>echo# <br>echo span style="Usernamefont-family: $(whoami)"<br>echo<br>echo "Current Directorycourier;font-weight: $(pwd)bold"<br>echo<br><sed '/span><br># Save your editing changes and exit your text editor.<br><br># Issue the / d'''chmod''' command to add execute permissions for the user for the '''command-substitution~murray.bash''' filesaul/uli101/stuff.txt</span><br><br># Issue the following to run the user-variables.bash Bash shell script:<br><span style="colorfont-family:bluecourier;font-weight:bold;font-family:courier;">sed 's/line/NUMBER/g' ~murray.saul/command-substitutionuli101/stuff.bashtxt</span><br><br>Confirm that your shell script displays the correct information for your Matrix account.<br><br>
'''Part B: Writing Linux Commands Using the sed Utility'''
# Confirm that you are located in your Write a Linux sed command to display only lines 5 to 9 for the file: '''home~murray.saul/uli101/stuff.txt''' directory in your Matrix account.<br><br># To demonstrate Write a Linux sed command to display only lines the begin the need pattern “and” for the file: '''( )~murray.saul/uli101/stuff.txt''' construct, issue the following:<br><span style="color:blue;font-family:courier;font-weight:bold">echo "1 + 2"</span><br><br>What did you notice?<br><br># Issue Write a Linux sed command to display only lines that end with a digit for the following (using that math construct):<br><span style="colorfile:blue;font-family:courier;font-weight:bold">echo "$((1 + 2))"</span><br><br>What did you notice?<br>The '''$''' in front of the construct '''expands''' the result of the calculation~murray.<br><br># Issue the following commands demonstrating other types of math calculations:<br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 - 3))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 * 3))"<saul/span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 uli101/ 3))"</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$((2 ** 3))"</span><br><br>'''NOTE:''' You may notice that '''dividing''' '''2''' by '''3''' shows a '''zero''' resultstuff. To perform decimal calculations would require<br>the use of other commands such as txt'''awk''' or '''bc''' (that technique '''NOT''' covered in this course).<br><br>You can use this math construct with variables as well.<br><br># Issue Write a Linux sed command to save lines that match the following commands demonstrating using the math construct with variables:<br><span style="color:blue;font-family:courier;font-weight:bold">num1=34</span><br><span style="color:blue;font-family:courier;font-weight:bold">num2=12</span><br><span style="color:blue;font-family:courier;font-weight:bold">echo "$pattern “line” ((num1 * num2upper or lowercase))"</span><br><br>What did you notice?<br><br>You can create variables and assign them values in for the math construction as well.<br><br># Issue the following commands demonstrating using the math construct with variables:<br><span style="color:blue;font-family:courier;font-weight:bold">num1=5</span><br><span style="color:blue;font-family:courier;font-weight:bold">num2=3</span><br><span style="color:blue;font-family:courier;font-weight:bold">((result = num1 ** num2))</span><br><span style="color:blue;font-family:courier;font-weightfile:bold">echo "The result is: $result"</span><br><br># Use a text editor to create a Bash shell script called '''dog-years~murray.bash'''<br><br># Add the following lines to the beginning of this file:<br><span style="font-family:courier;font-weight:bold;">#!saul/binuli101/bash<br>echo<br>dogFactor=7<br>read -p "Please enter your age (in years): " humanYears<br>((dogYears = humanYears * dogFactor))<br>echo "You age in dog-years is: $dogYears"<br>echo<br></span><br># Save your editing changes and exit your text editorstuff.<br><br># Issue the txt'''chmod''' command and save results (overwriting previous contents) to add execute permissions 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 you age to see what happens.<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~/murrayresults.saul/scripts/week10-check-3 | more</span><br><br>If you encounter errors, make corrections and txt'''re-run''' the checking script until you<br>receive a congratulations message, then you can proceed.<br><br>
<pre>
Line one.
This is the second line.
This is the third.
This is line four.
Five.
Line six follows
Followed by 7
Now line 8
and line nine
Finally, line 10
</pre>
'''Perform Write the Following Stepsresults of each of the following Linux commands for the above-mentioned file:'''
# <span style= LINUX PRACTICE QUESTIONS "font-family:courier;font-weight:bold">awk ‘NR = The purpose of this section is to obtain '''extra practice''' to help with '''quizzes''', your '''midterm''', and your '''final exam'''= 3 {print}’ ~murray.saul/uli101/stuff.txt</span><br><br> Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document tosimulate a quiz# <span style="font-family: httpscourier;font-weight:bold">awk ‘NR >= 2 && NR <= 5 {print}’ ~murray.saul/uli101/ictstuff.senecacollege.catxt</span><br><br># <span style="font-family:courier;font-weight:bold">awk ‘$1 ~ /This/{print $2}’ ~murray.saul/uli101/uli101_week10_practicestuff.docxtxt</span><br><br> Your instructor may take# <span style="font-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. efamily:courier;font-mailweight:bold">awk ‘$1 ~ /This/ {print $3, etc)$2}’ ~murray. '''Review Questions:''' '''PART A: WRITE BASH SHELL SCRIPT CODE''' '''Write the answer to each question below the question in the space providedsaul/uli101/stuff.''' txt</span><br><br>
# Write a Linux awk command to display all records for the file:'''cat walkthru1~/cars''' whose fifth field is greater than 10000.bash<br><br># Write a Linux awk command to display the first and fourth fields for the file: '''~/cars''' whose fifth field begins with a number.<br><prebr>#!Write a Linux awk command to display the second and third fields for the file: '''~/usr/bin/bashcars''' for records that match the pattern “chevy”.<br><br>word1=”counter”word2=”clockwise”echo “The combined word is# Write a Linux awk command to display the first and second fields for all the records contained in the file: $word2$word1”'''~/cars'''<br></prebr>
[[Category:ULI101]]