Reference no: EM133140282
KIT501 ICT Systems Administration Fundamentals - University of Tasmania
Goal
The main purpose of this third practical is to give you further familiarity with the UNIX shell (in particular, bash).
A Login to your ictteach account. Change into kit501 directory.
Remove any existing files or subdirectories under kit501 (unless you really want to keep them).
B Use joe or nano to create three text files which must be named as file1, file2, and file3, respectively.
The content of file1 must be:
Content of file1
(Note: press the Enter key at the end of the line before saving the content)
The content of file2 must be:
Content of file2
(Note: press the Enter key at the end of the line before saving the content)
The content of file3 must be:
Content of file3
(Note: press the Enter key at the end of the line before saving the content)
Use cat to verify the file contents after they have been created.
C Run the following command lines and think about the outputs:
$ ls
$ cat file1 > files
$ ls
$ cat files
$ cat file2 > files
$ ls
$ cat files
$ cat file3 > files
$ ls
$ cat files
Have you seen that redirecting output using > erases any pre-existing content?
Now let's try it again using a different approach to see how you can keep any pre- existing content of a file:
$ ls
$ cat file1 > files
$ ls
$ cat files
$ cat file2 >> files
$ ls
$ cat files
$ cat file3 >> files
$ ls
$ cat files
D It is possible to redirect a group of commands using a single command line:
$ rm files
$ ls
$ (cat file1; cat file2; cat file3) > files
$ ls
$ cat files
E Next, let's try redirecting both input and output. First of all, let's slightly change the content of files:
$ rm files
$ ls
$ (cat file3; cat file2; cat file1) > files
$ ls
$ cat files
$ sort < files
$ cat files
$ sort < files > files_sorted
$ ls
$ cat files_sorted
Can you see what has happened here?
F Redirecting error messages is different.
Ensure that you do not have a file named foo under the current directory. If you do, remove it first.
$ cat foo
$ cat foo 2> errors
$ ls
$ cat errors
G You have previously used the find utility to locate files on the system by specifying a set of criteria. You were told to get rid of the many "Permissions denied" error messages by redirecting them to the null device (/dev/null). What if you want to have a record of all the error messages generated from a find session?
$ find / -name "index.htm" 2> errors
(Note: this command line may take some time to run)
$ cat errors
H Remove the errors file.
I What is the difference between the following command lines:
$ cat file1
$ cat < file1
You see the same output from both command lines. The difference is this: In the first command line, the command cat opens the content of file1, but in the second command line, the shell opens the content of file1 and passes it to cat.
J Ensure that you do not have a file named foo under the current directory. If you do, remove it first. Then try the following two command lines and think about why there is a difference between the outputs of them:
$ cat foo 2> error1
$ cat < foo 2> error2
In the second command line, has the file error2 been created after the command is run?
Pipes
A Follow the exercises in section 1.2 to perform the following actions, and ensure that you understand the outputs.
B $ cat
$ cat
$ cat
$ cat files files files files
|
|
|
sort sort sort
|
|
wc wc
-l
-w
C $ who
$ who
| wc -l
|
wc
-l
$ who
$ ls
$ cat | tee userlist
userlist
$ who | tee userlist2
$ ls
$ cat userlist2
D What does the following command line do?
$ cat /etc/passwd | grep "David" | wc -l
Command Substitution
A Perform the following actions.
B $ echo This is a nice day
$ echo "This is a nice day"
$ echo 'This is a nice day' (note the single quotes)
C $ echo This tutorial is held on `date`
(note the back-quotes)
$ echo "This tutorial is held on `date`"
$ echo 'This tutorial is held on `date`'
D $ echo There are `ls | wc -l` files in this dir
$ echo "There are `ls | wc -l` files in this dir"
$ echo There are 'ls | wc -l' files in this dir
(note the single quotes)
$ echo There are \`ls \| wc -l\` files in this dir
E Make two subdirectories (under the current directory) named dir1 and dir2, respectively. Explore the behaviour of the following command line (note the option to tail is a one, not the letter l):
$ cd `find . -type d | tail -1`
You may wish to explore the tail command first.
Attachment:- UNIX shell.rar