14
NTEn 219 – Linux Server Management Network and Telecommunications Engineering Technology Fall Semester 2013 Lab Assignment 7 (Assigned October 17. Due at 5:00pm October 21, 2013) Student name: --------------------------------------------------- Objectives: 1. Learning the basics of Linux Shell Scripting 2. Filter Commands Open this Word file on your host OS (Windows 7). Type your answers and insert screenshots into answer boxes where indicated. When saving this file please use your first name and first letter of your last name, followed by “_Lab7” as the file name. For example, I would name the file AlanK_Lab7.docx. When you are done, save it and submit it to me via email ([email protected] ) as an attachment. When you have completed this Lab Assignment you can start working on your scripting assignment, which will be provided to you as a separate document. Task 1: Learning the basics of Linux Shell Scripting Part A - Given the following script content in a file: echo All: $@ shift echo "New first:" $1 shift echo "New first:" $1 echo "All:" $@ Use the vi editor to create the script file shown above and name it lab6a. NTEN 219 Lab Assignment 7 Page 1

NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

NTEn 219 – Linux Server ManagementNetwork and Telecommunications Engineering Technology

Fall Semester 2013Lab Assignment 7

(Assigned October 17. Due at 5:00pm October 21, 2013)

Student name: ---------------------------------------------------

Objectives: 1. Learning the basics of Linux Shell Scripting2. Filter Commands

Open this Word file on your host OS (Windows 7). Type your answers and insert screenshots into answer boxes where indicated. When saving this file please use your first name and first letter of your last name, followed by “_Lab7” as the file name. For example, I would name the file AlanK_Lab7.docx. When you are done, save it and submit it to me via email ([email protected]) as an attachment.

When you have completed this Lab Assignment you can start working on your scripting assignment, which will be provided to you as a separate document.

Task 1: Learning the basics of Linux Shell Scripting

Part A - Given the following script content in a file:

echo All: $@shiftecho "New first:" $1shiftecho "New first:" $1echo "All:" $@

Use the vi editor to create the script file shown above and name it lab6a. Change the permissions of the file to add executable permission for everyone:

> chmod +x lab6a

Clear the screen and run the script as follow:

> clear> ./lab6a Eenie Meenie Miney Mo

NTEN 219 Lab Assignment 7 Page 1

Page 2: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Take a screenshot of the results generated by the script and place it here:

Please describe how these results were generated from the script.

Part B - Given the following script content in a file:

a=$(echo $1 | cut –c 1-2)echo First two letters are $als=xyzecho lsecho $lsecho $(ls)xyz=lsecho $($xyz)

Use the vi editor to create a script file called lab6b. Add executable permission, clear the screen and run the script as follows:

> clear> ./lab6b Hello

NTEN 219 Lab Assignment 7 Page 2

Page 3: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Take a screenshot of the results and place it here:

Please describe how these results were generated from the script.

Part C - Given the following script content in a file:

abc=10echo 1+4 $1+4 $1+$abcecho $((1+4)) $(($1+4)) $(($1+$abc))echo $((1+1))+$((2+2))echo $(1+1)

Create the script file and name it as lab6c1. Make a copy of lab6c1 and name the copy lab6c2. Then edit lab6c2 to replace all

mathematical evaluation portions with the expr utility program. Add executable permission to both lab6c1 and lab6c2. Then, clear the screen and run them as follows:

> clear; ./lab6c1 123; ./lab6c2 123

Are they both producing identical outputs? If so, then take a screenshot of the results in the terminal and insert it inside the box below:

NTEN 219 Lab Assignment 7 Page 3

Page 4: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Screenshot here:

Please explain what happened to the last line (echo $(1+1) ) of the script file.

NTEN 219 Lab Assignment 7 Page 4

Page 5: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Part D - Given the following script content in a file:

if [ $# -ne 1 ]then echo "Usage: $0 FILENAME" exit 1elif [ ! -f $1 ]then echo "The file '$1' doesn’t exist!" exit 1fi

words=0for a in $(cat $1)do# echo "["$a"]"words=$((1+$words))done

echo "Num of words is" $words

Put the above code into a file called lab6d, and add executable permission to lab6d. Then run it as follow:

> clear; ./lab6d lab6d

Please describe how the lab6d script works below:

NTEN 219 Lab Assignment 7 Page 5

Page 6: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Task 2: Using Filter Commands

UNIX has a variety of so-called filter commands that allow you to read an input stream and filter it based on a variety of criteria before sending it to an output stream. Combing these filter commands using pipes provides a powerful means of building some very useful customized commands. We will look at four filter commands:

sort grep cut tr

We will experiment with these four filter commands in this section. In order to have some data to filter, put the following data in a file called test.dat. Be sure to use the same spacing that I have used (use spaces, not tabs). And, make sure there are no empty lines at the end of the file.

halibut Meat 22.45apples Produce 1.99soap Grocery 12.50hammers Hardware 22.19steak Meat 12.86lobster meat 34.19bananas Produce 2.99milk Grocery 5.79pears Produce .99

sort Command

The sort command can be used to sort data in an input stream in ascending order or descending order. Try the following sequence of commands:

cat /etc/passwd cat /etc/passwd | sort cat /etc/passwd | sort -r

You will notice that the second command took the output of the first command and sorted it in ascending order. The third command reversed the order of the lines of the /etc/passwd file displayed on the screen. The sort command used with the -r option specifies that a reverse-order sort should be used. Remember that sort and other filter commands are not changing the data in the /etc/passwd file itself. Instead, sort is just filtering the output of the cat command.

Try using sort on the test.dat file: cat test.dat | sort

NTEN 219 Lab Assignment 7 Page 6

Page 7: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

grep Command

The grep command is used to display lines that contain a pattern. In this case we are filtering out lines that don’t contain the pattern. Try the following commands:

cat test.dat cat test.dat | grep Produce cat test.dat | grep Meat cat test.dat | grep -i Meat

The first command sends the output of the test.data file to the monitor. In the other commands, the output of cat is sent to grep to be filtered. In the second command, all lines that contained the pattern “Produce” made it through grep’s filter. If you entered the test.dat file exactly as shown, you should see that the third and fourth commands generated different output. The third command generated two lines of output while the fourth line generated three. The line of test.dat that contains “lobster” had the word meat spelled with a lowercase ‘m’ and did not match the pattern Meat. As we can see grep is case sensitive. In the fourth command we used the –i option which requests that grep be used in a case-insensitive mode.

So, above we used grep to answer the questions, “Which foods came from the Produce department?” and “Which foods came from the Meat department?” But, what if we wanted to ask the question, “What are the foods that came from any department other than Produce?” In this case we want all the lines that don’t contain the pattern Produce. To do this we can use the –v option. Try this:

cat test.dat | grep -v Produce

We see that every line not containing the pattern Produce is displayed.

Another useful trick with grep is to use it to eliminate empty lines from a stream: ... | grep –v "^$"

cut Command

The grep command was used to horizontally filter data – it kept some lines and eliminated others. The cut command is used to vertically filter data. We can cut data based on a count of characters, or we can cut based on so-called field. Try these commands

cat test.dat | cut –c 10-19 cat test.dat | cut –c 1-9 cat test.dat | cut –c 2-10 cat test.dat | cut –c 3-11

Continue to change the two numbers on each line until you get a feel for the way cut is filtering the data. See if you can get just the third field to display.

You see that cut with the –c option allows you to cut the data vertically based on character position within the line. Have a look at the /etc/passwd file:

cat /etc/passwd

NTEN 219 Lab Assignment 7 Page 7

Page 8: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

Here are some lines within my /etc/passwd file:

amanda:x:37:6:Amanda admin:/var/lib/amanda:/bin/bashat:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bashbin:x:1:1:bin:/bin:/bin/bashdaemon:x:2:2:Daemon:/sbin:/bin/bash

Unfortunately, if we wanted to cut the finger information (the 5th field) out, we could not use cut with the –c option as each field is of varying length. However, each field is separated (or, delimited) with the ‘:’ character. The cut command has an option that allows you to cut based on a delimiter. Try this:

cat /etc/passwd | cut –d: –f5

You see that the finger information has been extracted as we wanted. To use cut in this manner, we use the –d option which should be followed by the delimiter character. Then we follow that with –f and the field that we want cut. Since the finger information is the 5th field we used –f5. With this type of cutting, we can extract any columns we want. Try this:

cat /etc/passwd | cut –d: –f1,5,7

You see that we get field 1 (username), field 5 (finger data), and field 7 (default shell). The fields in the output are separated by the same delimiter character. What if we want to change the delimiter character to something else, such as a comma or a space? We use the tr command.

tr Command

The tr command (short for translate) is a command that translates individual characters in the input stream to other characters in the output stream. For example, try the previous command filtered through tr as follows:

cat /etc/passwd | cut –d: –f1,5,7 | tr : ,

The tr filter says replace every occurrence of the ‘:’ character with the ‘,’ character. If we wanted to replace the ‘:’ with the space character, we would do this:

cat /etc/passwd | cut –d: –f1,5,7 | tr : " "

Notice that we have to quote the space character. Otherwise the shell would not know there was a token to be processed. Try experimenting with tr on the test.dat file.

cat test.dat | tr e x

There is another useful thing that tr can do. It can eliminate repeating characters in a stream. Try to use the –d feature of the cut command on the test.dat file like this:

cat test.dat | cut –d" " –f1,3

You will see that you don’t get the third field as expected. What is happening here is that each individual space character is being considered a delimiter. So, when you specify field 3, usually you get nothing because most lines have three spaces following the first field. What would be helpful is if we had a mechanism to eliminate repeating space characters. Try this:

cat test.dat | tr –s " "

Here we ask tr to run with the –s option (the so-called squeeze option), which eliminates repeating occurrences of the character specified. In this case we specified the space character (in quotes). The output shows each field of the test.dat file separated by a single space character. The output is no longer lined up,

NTEN 219 Lab Assignment 7 Page 8

Page 9: NTEN 219 - people.okanagan.bc.capeople.okanagan.bc.ca/akennedy/courses/f2013/nten219/labs/La… · Web viewFilter Commands. Open this Word file on your host OS (Windows 7). Type your

but now we can use the output of this command as the input to the cut –d command to get the first and third fields as follows:

cat test.dat | tr -s " " | cut -d" " -f1,3

Finally, the tr command can also be used to delete characters from a stream. In this case you would use the –d option. Here is an example:

cat test.dat | tr -d " "

Exercise: Experiment with the output of the ls –l command and the cut, tr, and sort filters to see if you can display a listing of the files in one of your directories so that it shows the size of each file followed by the filename. For example, suppose my directory contains the files foo1, foo2 and stuff as shown below:

bob@nten219Server:~/lab7/temp> ls -ltotal 12-rw-r--r-- 1 bob users 193 2012-10-17 21:40 foo1-rw-r--r-- 1 bob users 43 2012-10-17 21:40 foo2-rw-r--r-- 1 bob users 150 2012-10-17 21:39 stuff

Create a command line that, if executed, would generate the following output on the above directory:

193 foo143 foo2150 stuff

Put your command here:

That’s it for Lab Assignment 7

If you have more time, you can start working on your Scripting Assignment.

NTEN 219 Lab Assignment 7 Page 9