Shell scripting: few questions related to I/O redirection
Not what you're looking for?
1. Will the following only print the text "I FOUND A MATCH" to standard output when the grep is successful?
if grep "mrichard" /etc/passwd; then echo "I FOUND A MATCH"; fi
2. Does the following command send both standard output and standard error to the same file for the command cmd1?
cmd1 2>&1 >outfile
3. Does cmd2 receive both the standard error and standard output of cmd1 as standard input?
cmd1 2>&1 | cmd2
4. Will the script below always print out the text 'They are the same' assuming the script ran under the Bourne shell?
#!/bin/sh
infile=${0}
if [ ! -s "$infile" ]
then
exit 1
fi
linecnt=`wc -l $infile | while read cnt rest
do
echo $cnt
done`
mycnt=0
while read line
do
mycnt=`expr $mycnt + 1`
done < $infile
if [ $linecnt -eq $mycnt ]
then
echo They are the same
else
echo They are different.
fi
5. Does the standard output of this script get sent to STDOUT?
#!/bin/sh
exec 4>&1
exec 1>&2
exec 2>&4
exec 4>&-
echo Hello
echo More text
6. Is the visible output of this script, the text 'Hello'?
#!/bin/sh
(
exec 4>&2
exec 2>&1
exec 1>&4
exec 4>&-
echo Hello
) 2> /dev/null
Purchase this Solution
Solution Summary
Solution gives sufficient explanation for the answers to I/O redirection questions.
Solution Preview
1. No. It will also print all the lines containing mrichard.
2. No. It sends only standard output to outfile, because the standard error was duplicated as standard output (i.e. file descriptor 2 was made to be a copy of file descriptor 1) before standard output redirection.
3. Yes. ...
Purchase this Solution
Free BrainMass Quizzes
Javscript Basics
Quiz on basics of javascript programming language.
Basic UNIX commands
Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.
Basic Computer Terms
We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.
Basic Networking Questions
This quiz consists of some basic networking questions.
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.