Out: Jan 24th, 2007
Due: Jan 31st, 2007
CTEC203 – Lab 3
1.The purpose of this exercise is to give you practice with shell metacharacters for redirecting input and output. At the end you should understand the use of > > < < 1> 2> and |
Run the following sets of commands. Observe what is output to the screen and what is output to a file. A short 1-2 line English explanation of what's happening is all that's needed. The paired commands are similar, but different in some way – note the differences.
a)Explain he difference in the result of the following sets of commands?
i.cal 2 2006 > Feb; cal 2 2007 > Feb; cal 2 2008 > Feb
ii.(cal 2 2006; cal 2 2007; cal 2 2008) > FebV2
b)What is the difference in the result of the following commands? Note that the file Feb should still exists from 1a) and that the file March should not exist.
i.cat Feb March 1> result-b1
ii.cat Feb March 2> result-b2
c)What is the difference in the result of the of following sets of commands. Explain why:
i.cat Feb March 1> result-c1 2> result-c2
ii.cat Feb March 2> /dev/null
d)What is the difference in the result of the following sets of commands.
i.date > logfile; date > logfile; date > logfile;
ii.date > logfile2; date > logfile2; date > logfile2
e)The 1st two commands have the same effect – explain what each one of them is doing. The 3rd command does something different – after you run it check the files in your directory and then explain what happened.
i.(who; ls) | cat -n
ii.cat -n <(who; ls) #Note: the characters “<(“ must be next to each other.
iii.(who; ls) > cat
f)Refer to the man pages on the tee command and pay attention to the -a option. Run the following commands and check the contents of the result file after each one. Explain the contents of the resulting files, especially the number of lines
i.date | tee r1 r1 r1
ii.date | tee -a r2 r2 r2
g)Type in the following commands, enter a few lines of text and then press <CTRL>D (end of file.) Explain what the commands do
i.cat </dev/tty >/dev/tty
ii.cat > simpleEdit
iii.cat | tee copy1 copy2 >copy 3
h)Type in the following example of a here file. The single quotes uses are backquotes (upper left corner or the keyboard under the tilde), not regular single quotes. They are explained in Ch. 6 of the text. Expressions inside backquotes are taken as Unix commands and the result of running them is substituted into the text. The final EOF must start in column 1.
i.cat <EOF
Now is the time `date +”%T” `
for all good men and women such as `whoami` tojj
come to the aid of their $SHELL
Signed,
$USER and `who | wc -l ` others
EOF
ii.Test and show the syntax to redirect the output of a here file to the wc command. To another file. The trick is to figure out where to position | wc – either on the first line or the last line. Use something simple such as as the basis for your exploration.
cat <EOF
This is a test
EOF
2.Refer to the manual pages on date. (man date) (8 marks)
a)What is the difference in output between the following versions of the date command
i.date
ii.date -u
b)Issue the following command and record the results. Hilite or underline the differences in output:
i.export TZ=Singapore
date
ii.export TZ=Egypt
date
c)Refer to the directory /usr/share/zoneinfo. To reset the current timezone issue the command: export TZ=filename where filename is the name of a file in this directory. If the timezone file is in one of the subdirectories you need to use the file name relative to the zoneinfo directory: ie: export TZ=Canada/Yukon. Search thru the directories and set TZ to the timezone for Khartoum. Note the time (and date) for the time here in Toronto and for the time and date in Kartoum. Also find the file name and figure out the time that represents where you were born. (If you were born in the same time zone as Toronto, use the Samoa)
d)Issue the following command: date + “%a day %c of %B”
Refer to the man pages fore the date command. Describe the effect of each of the format codes used. (Since Unix is written in C, it shouldn't surprise you to find that C makes use of these format codes as well – you will find the same list of codes in your C text (Prata, 5th edition) on pp 860-861.
e)What is the command to produce a date with the following (sample) format:
It's Friday the 200th day of year 6 after the millenium.
f)What is the command to produce a date with the following format:
Week 3. Day 6. 14 hours and 26 minutes after midnight
(Note: put the command in your .bashrc file and you can have it print out the date every time you log in or start a new shell.)
g)The date command can also produce dates other than the current date. The format for this is: date -d “Jan 31, 2006 8:45:00 am” . Try this for the date you were born and find out what day of the week it was. The earliest possible date is Jan 1st, 1970 This is known as the EPOCH and is supposed to be the birth date of Unix. Experiment and find out what the latest possible date and time in Unix is supposed to be. (Hint – its between 30 and 40 years from now.
h)Use the date command to generate the month and year you were born, ie: 01 1987.
Insert this command into the cal command using backquotes
ie: cal `date -d “birthdate” +”format codes” `
What was the result? (Done properly this should produce a meaningful answer.)
i)Use the “%s” format of the date command to generate # of seconds since the epoch of your birth date. this assignment is due at 12:40 pm on Jan 31st, 2007. How old will you be then, in seconds. (If you were born before the epoch, use April 7, 1991. If you do not know the time of your birth, use 6:38 am. Please record all commands and calculations used.