Lab 5 Reading Read about standard input and standard output starting on Sobell,
ID: 3572797 • Letter: L
Question
Lab 5
Reading
Read about standard input and standard output starting on Sobell, page 133, up to the section on noclobber on page 139.
Procedure
1. “The Keyboard and Screen as Standard Input and Standard Output” on page 135 of Sobell explains how to use the cat utility to read from standard input (defaults to the keyboard) and write to standard output (defaults to the screen). Use cat as shown in Figure 5-5 on page 135 of Sobell to copy standard input to standard output. Press CONTROL-D on a line by itself to terminate cat.
2. The echo builtin copies its arguments to standard output which, by default, bash directs to the screen.
$ echo This message goes to standard output.
This message goes to standard output. The cat utility sends the contents of a file specified by its argument to standard output.
$ cat /etc/host etc/hosts
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Redirect standard output (Sobell, page 136) of echo to write a short message to a file and then use cat to display the contents of the file.
3. As demonstrated in step 1, when you do not specify an argument for cat, cat copies standard input to standard output. Redirect standard input to cat to come from the file you created in the previous step. Do not redirect standard output from cat; it will appear on the screen.
4. Redirect the output of an ls –l command to a file named ls.out. Display ls.out using cat.
5. The who utility (Sobell, page 70) lists the users who are logged in on the system. Append the output (Sobell, page 140) of the who utility to the ls.out file you created in the previous step. Display the augmented ls.out file using cat.
What happens if you omit one of the greater-than signs (use > in place of >>)? Try it and see.
6. Redirect standard output of cat to create a file named days that holds the names of the days of the week in chronological order, one per line. Do not redirect standard input to cat; it will come from the keyboard. Remember to press CONTROL-D on a line by itself to exit from cat.
Use cat to read the days file and send it to standard output, through a pipeline, to standard input of the sort (Sobell, pages 58 and 143) utility. The result will be a list of days in alphabetical order.
Replace sort in the preceding command with grep (Sobell, page 56) with an argument of (uppercase) T. The result will be a list of days that have an uppercase T in their names in chronological order.
Next create a filter (Sobell, page 144) by repeating the preceding command but sending standard output of grep through a pipeline to standard input of sort. The result will be a list of days that have an uppercase T in their names in alphabetical order.
7. Produce a long listing of the /etc, /usr/bin, and /sbin directories, sending the output to a file and running the command in the background (Sobell, page 146).
Deliverables
This lab gives you practice redirecting standard input and standard output on the command line. Copy all commands and output in a text file and submit to your instructor.
Explanation / Answer
The 1 and 2 does't look like questions, hence answering from 3
3. As demonstrated in step 1, when you do not specify an argument for cat, cat copies standard input to standard output. Redirect standard input to cat to come from the file you created in the previous step. Do not redirect standard output from cat; it will appear on the screen.
cat > 1.txt
My name is Bob //Press ctrl-d simultanously once
//We can create wile with the redirection operator ">" as well as shown above
cat 1.txt
My name is Bob
4. Redirect the output of an ls –l command to a file named ls.out. Display ls.out using cat.
ls -l > ls.out
cat ls.out
//We use ">" symbol to redirect the o/p of a command to file ls.out and to display the content of file cat is used
5. The who utility (Sobell, page 70) lists the users who are logged in on the system. Append the output (Sobell, page 140) of the who utility to the ls.out file you created in the previous step. Display the augmented ls.out file using cat.
who >> ls.out
cat ls.out
// the ">>" symbol is used for appending to the already exisitng file ls.out
Redirect standard output of cat to create a file named days that holds the names of the days of the week in chronological order, one per line. Do not redirect standard input to cat; it will come from the keyboard. Remember to press CONTROL-D on a line by itself to exit from cat.
Use cat to read the days file and send it to standard output, through a pipeline, to standard input of the sort (Sobell, pages 58 and 143) utility. The result will be a list of days in alphabetical order.
Replace sort in the preceding command with grep (Sobell, page 56) with an argument of (uppercase) T. The result will be a list of days that have an uppercase T in their names in chronological order.
Next create a filter (Sobell, page 144) by repeating the preceding command but sending standard output of grep through a pipeline to standard input of sort. The result will be a list of days that have an uppercase T in their names in alphabetical order.
cat > days
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
[ctrl-D] //Need to press ctrl-D simultanously once
cat days | sort
//Output will be as shown below in alphabetical order
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
cat days | grep T
//Output will be the days having uppercase "T" in it
Tuesday
Thursday
7. Produce a long listing of the /etc, /usr/bin, and /sbin directories, sending the output to a file and running the command in the background (Sobell, page 146).
//& is use run the process in background, We can list the file simultaneously for all the directories.
ls -l /etc /usr/bin/ /sbin/ > ls.out &
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.