CSC3300 - Lab Activity 3
CSC3300 - Lab Activity 3
1 2023/2024
LAB 3
INSTRUCTIONS:
~$ ls > myoutput.txt
~$ ls >> myoutput.txt
~$ ls < myoutput.txt
(Note: The command will be the same as ls myoutput.txt (without the ‘<’), since
the ls command already expecting the first parameter to be the input file.)
1
CSC3300 Sem. 1 2023/2024
Questions:
1. Describe the examples of tasks that might require the use of the redirection
operators (>, >>, <)?
‘>’: Used to send information to a file
‘>>’: Used to send information to the end of the file instead of overwriting it
‘<’: Used to read the input for a command from a file
The use of pipe ‘|’ is to allow programs to share data (e.g. to allow a program to share
its output to become input for another program/command). Type the following
command which connect ls command with head command,
~$ ls | head -3
The number of pipes can be more than one. Type the following command, which runs
the command ls and send its output as the input for head command, and then send its
output to be the input for tail command.
~$ ls | head -3 | tail -1
2
CSC3300 Sem. 1 2023/2024
Questions:
1. Use man to learn more about the use of ls, head, tail and then describe the
purpose of using the switches (-3 for head, and -1 for tail).
ls Command:
To access the manual page for ls, you can use the following command: man ls.
The ls command is used to list files and directories in a directory.
It has several options, such as -l for a long listing format, -a for displaying hidden
files, and -h for displaying file sizes in a human-readable format.
head Command:
To access the manual page for head, you can use the command: man head.
The head command is used to display the first few lines of a text file.
There are no options like -3 for head. By default, it displays the first 10 lines of
the file.
tail Command:
To access the manual page for tail, you can use the command: man tail.
The tail command is used to display the last few lines of a text file.
There are no options like -1 for tail. By default, it displays the last 10 lines of the
file.
2. Give an example of other instruction that can be used with pipes. Show how you
use it, and describe the objective/purpose.
Suppose you have a directory named text_files with multiple text files, and you
want to find unique words and their occurrences across all these files. You can use
the following command:
cat text_files/*.txt reads the content of all text files in the text_files
directory and combines them into a single stream.
tr -s '[:space:]' '\n' replaces spaces and other whitespace characters with
newline characters. This effectively splits the text into individual words.
sort sorts the words alphabetically.
uniq -c counts the unique words and their occurrences.
sort -nr sorts the unique words by their frequency in descending order.
3
CSC3300 Sem. 1 2023/2024
3. Process Management
Task: Creating Process
Create a process by running any program or user application.
Use the ps command to list the currently running processes. Then use the kill
command to terminate a process by supplying the process’ id. Assuming there is a
Vim process with ID (PID) 2390, signal -9 will caused the program to be terminated
instantly. Type the following command,
~$ kill -9 2390
We may also use the pidof command to determine a known process. For example, we
know a Vim program is running, use the following command to determine the PID of
the program.
~$ pidof vim
Questions:
1. Use man to learn more about the use of kill and then describe the three more
switches (other than -9) that can be used with kill command.
kill – send a signal to a process
Switches:
-l: list signal names. This option has optional argument, which will convert signal
number to signal name, or other way around.
-L: list signal names in a nice table
-s, --signal<signal>: specify the signal to be sent. The signal can be specified by
using name or number.
4
CSC3300 Sem. 1 2023/2024
Use vimtutor to learn more about Vim (commands for editing file, and also how you
can save and open an existing file).
~$ vimtutor
Learn from the vimtutor. Then recall the Vim with fg command and practice creating
editing and saving text file.
Questions:
1. Specify three Vim commands that you can use in editing
Deletion: Press x to delete the character under the cursor
Insertion: Press i to insert text
Appending: Press A to append text
Shell scripting is a powerful facility provided by Bash (and many other shells in other
operating systems). With the experience from the exercise, students are encouraged to
explore features like using variables, branching and looping command provided in Bash’s
shell scripting to produce more advanced process management tasks such as batch
commands, scheduled processing or automatic software installation/setup.