In: Computer Science
Exercise.
Practice redirection to change the above commands so that:
(i) error messages are sent to /dev/null, but
(ii) we still detect errors and halt execution of the sequence appropriately.
For i, recall from prior labs that standard error stream (stderr) is #2, and you will need redirection to route an error normally to stderr to /dev/null.
Hint: Recall that redirection happens per process executed. So, what do you need to add to each process to make the redirection successful?
Below are 4 commands to rewrite:
#1) ls . && ls -! && ls -al || echo "We Failed" #2) ls -! && ls . && ls -al || echo “We Failed” #3) ls . && ls -al && ls -! || echo "We Failed" #4) ls . && ls -al || echo “We Failed” |
Write a new version of each by copying the list below, and add your revisions to the four above commands:
1.
2.
3.
4.
## Hey bud, here are your commands and explaination. Feel free to ask any doubt. Consider giving a thumbs up. ThankYou :)
# The standard error descriptor is 2.
# To pipe any error to /dev/null we use 2>/dev/null
# If there are multiple statements, then 2>/dev/null has to be used for each one of them
# Or better way to do it is listed below.
# Edited Commands ( Normal Way)
ls . 2>/dev/null && ls -! 2>/dev/null && ls -al 2>/dev/null || echo "We Failed"
ls -! 2>/dev/null && ls . 2>/dev/null && ls -al 2>/dev/null || echo “We Failed”
ls . 2>/dev/null && ls -al 2>/dev/null && ls -! 2>/dev/null || echo "We Failed"
ls . 2>/dev/null && ls -al 2>/dev/null || echo “We Failed”
# Edited Commands (More Efficient Way)
{ ls . && ls -! && ls -al } 2>/dev/null || echo "We Failed"
{ ls -! && ls . && ls -al } 2>/dev/null || echo “We Failed”
{ ls . && ls -al && ls -! } 2>/dev/null || echo "We Failed"
{ ls . && ls -al } 2>/dev/null || echo “We Failed”