In: Computer Science
can you please do the following?
(8) Step 8: type the following command:
sort /etc/passwd | head -5
What is the output?
Notice that this pipe can be simplified
cat /etc/passwd | head -5
What is the output for this?
You could accomplish the same thing more efficiently with either of the two commands:
head -5 /etc/passwd
head -5 < /etc/passwd
(9) Step 9:
The command displays all the files in the current directory sorted by file size
ls -al | sort -n -r +4
What is the output? What are the outputs sorting according to? Which column is this?
The command ls -al writes the file size in the fifth column, which is why we skip the first four columns using +4.
The options -n and -r request a numeric sort (which is different than the normal alphabetic sort) in reverse order
awk
Topics covered: processing columnar data
Utilities covered: awk
The awk utility is used for processing columns of data
(10) Step 10:
The following example shows how to extract column 5 (the file size) from the output of ls -l
ls -l | awk '{print $5}'
What is the output?
Cut and paste this line into a Bourne shell and you should see a column of file sizes, one per file in your current directory.
(11) Step 11:
The following example shows how to sum the file sizes and print the result at the end of the awk run
ls -al | awk '{sum = sum + $5} END {print sum}'
In this example you should see printed just one number, which is the sum of the file sizes in the current directory.
Shell Scripts
Topics covered: storing commands in a file and executing the file
Utilities covered: date, cal, last (shows who has logged in recently)
(12) Step 12:
Store the following in a file named simple.sh and execute it
#!/bin/sh
# Show some useful info at the start of the day
date
echo Good morning $USER
cal
last | head -6
What is the result after you execute it?(Shows current date, calendar, and a six of previous logins Notice that the commands themselves are not displayed, only the results )
last command - display login and logout information about users and
terminals
(13) Step 13:
To display the commands verbatim as they run, execute with
sh -v simple.sh
Another way to display the commands as they run is with -x
sh -x simple.sh
What is the difference between -v and -x? Notice that with -v you see '$USER' but with -x you see your login name
Run the command 'echo $USER' at your terminal prompt and see that the variable $USER stores your login name
With -v or -x (or both) you can easily relate any error message that may appear to the command that generated it
(14) Step 14:
When an error occurs in a script, the script continues executing at the next command
Verify this by changing 'cal' to 'caal' to cause an error, and then run the script again
Run the 'caal' script with 'sh -v simple.sh' and with 'sh -x simple.sh' and verify the error message comes from cal
Other standard variable names include: $HOME, $PATH, $PRINTER. Use echo to examine the values of these variables
Step 8
************
Step 9
***********
Step 10
**********
Step 11
************
Step 12
***********
Step 13
*************
Step 14
***********
if you have any doubt then please ask me without any hesitation in the comment section below, if you like my answer then please thumbs up for the answer, before giving thumbs down please discuss the question it may possible that we may understand the question in a different way and I can edit and change the answers if you argue, thanks :)