Explore BrainMass
Share

Bash shell scripting questions

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

1) Describe the result of executing each of the following grep commands.

a. grep -c ill memo
b. grep -n ill memo

2) Rewrite the following sequence of commands using sort as a filter. The end result should be a single command line when you are done.

sort roster > temp
lp temp
rm temp

3) Write a command to list all files in current directory, that:

a. Start with the letters b or f.
b. Start with the letters a through k.
c. Have a capital letter anywhere in their name.

4) What is the difference between the following commands?

cat xyz[12]
cat xyz[1-2]

5) Listing for which files will be given by the following command?

ls -l file[1-20]

6) Write a Bash script that will prompt for the user's first and last names and store the responses in two variables. Then it will display the stored information in the format "You entered lastname, firstname." and ask the user for confirmation. If the answer is "y" or "yes", then say "Thank you!", and if the answer is "n" or "no" then start again with the prompts.

© BrainMass Inc. brainmass.com October 25, 2018, 12:28 am ad1c9bdddf
https://brainmass.com/computer-science/files/bash-shell-scripting-questions-229319

Solution Preview

1)
a. "grep -c ill memo" gives the count of number of lines in the file "memo" that have string "ill" occuring anywhere in them.
b. "grep -n ill memo" prints all the lines in the file "memo" that have string "ill" occuring anywhere in them. Lines are prepended with the corresponding line number separated by a colon.

For example, consider the file "memo" with the following ...

Solution Summary

Solution also explains the outcome of commands in many cases, giving examples in certain cases. Script implementation makes use of while loop to achieve the goal.

$2.19
See Also This Related BrainMass Solution

Briefly answer the questions related to I/O redirections in Bourne shell.

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

View Full Posting Details