CS 497C - Quiz 4 Sample Questions
Part 1:
(3) 1. When someone wants to "talk" to you but gets the connection refused
message, how could you possibly fix it?
(1) msg y (2) msg n (3) mesg y (4) mesg n
(1) 2. Which command reveals details of users?
(1) finger (2) w (3) who (4) detail
(4) 3. Which command can be used to transfer files between different hosts?
(1) rsh (2) rlogin (3) ssh (4) ftp
(2) 4. When you look for a pattern in files, but are not sure of the case,
which one will you use?
(1) grep -v (2) grep -i (3) grep -l (4) grep -e
(2) 5. Frame regular expression to match lines containing Mcgee mcghee
magee.
(1)[Mm]a?c?gh*ee (2) [Mm][ac]gh*ee (3) [Mm]a?c?gh?ee (4) [Mm]a*c*gh?**
(4) 6. How do you locate lines in the file foo that are less than 100
characters in length?
(1) grep ".\{100,\}" foo (2) grep -v ".\{0, 99\}" foo
(3) sed "/.\{100,\}/p" foo (4) sed "/.\{100,\}/d" foo
(4) 7. How do you select all but the last line of a file?
(1) sed "$p" foo (2) sed -n "$p" foo (3) sed "$p" (4) sed -n "$!p"
(1) 8. If x has the value 10, what are the values of x$x$?
(1) x10$ (2) 1010 (3) 10x$ (4) x$10
(1) 9. What can be put into the first line of your script to make sure a
script will use the C shell?
(1) #!/bin/csh (2) !/bin/csh (3) #$/bin/csh (4) $/bin/csh
(3) 10. Which one is the here document?
(1) >1 (2) <1 (3) << (4) >>
(2) 11. How can you make a variable x defined in the login shell available in
a shell script?
(1) set x (2) export x (3) eval x (4) set -o x
(3) 12. To debug shell script, what is put into the beginning of the
script?
(1) set -dg (2) set -u (3) set -x (4) set -w
Part 2:
1. What is an IP address? What is a domain name? Give an example for
each case.
Ans: 1) An IP address is the address that every host in the network has
and is used by other machines to communicate with it.
2) A domain name locates an organization or other entity on the Internet.
3) 156.26.10.43 is an IP address. www.cs.twsu.edu is a domain name.
2. What is a port number for? In which file are known port numbers
specified? Give at least two examples.
Ans: 1) A port number is a number which is used to identify a TCP/IP service.
Daemons listen for requests at certain specific port numbers assigned
to them.
2) Know port numbers are defined in /etc/services.
3) sendmail listens on port 25, ftp on 21 and telnet on 23.
3. What's the difference between a wild card and a regular expression?
Ans: A wild card is a special character understood and interpreted
differently by the shell, but the regular expression characters are
used by the command and has nothing to with the shell.
4. What does this command do? What are the two $s doing here?
grep "$SHELL$" /etc/passwd | cut -d: -f1
Ans: This command locates from /etc/passwd those users who use the same
login shell as the user executing the command. The command matches
the value of $SHELL at the end of /etc/passwd.
4. What is the sed command? Describe the format of the sed command.
Give an example.
Ans: 1) sed is a multipurpose tool which combines the work of several
filters.
2) sed options 'address action' file(s)
3) sed -n '1,$s/Basic/Java/g' foo
5. If a script is run as foo -l -t bar[1-3], what are the values of $#
and $*? Does it make any difference if the options are combined?
Ans: $# is 5 and $* expands to the string -l -t bar1 bar2 bar3 provided
all the files exist. Combining the options brings down $# to 4 and
changes $* to -lt bar1 bar2 bar3.
6. Write a script that accepts one or more filenames as arguments and
converts the filename to uppercase.
Ans: for file in $* ; do
ufile=`echo $file | tr '[a-z]' '[A-Z]'`
mv $file $ufile
done
7. What's wrong with this statement? How do you modify it to execute
correctly?
[ $# -ne 2 ] && echo "Usage: $0 min_guid max_guid" ; exit
Ans: The exit statement will always be executed irrespective of the
condition. Use the curly breaces to group commands and run exit in the
current shell:
[ $# -ne 2 ] && { echo "Usage: $0 min_guid max_guid" ; exit; }
8. Write a shell function for rm which goes to the interactive mode
whenever you use it with more than three filenames.
Ans: rmi () {
option= [ $# -gt 3 ] && option="-i"
rm $option $*
}