Thursday, February 14, 2008

Test Your Knowledge of Regular Expressions and Shell Basics

Test Your Knowledge of Regular Expressions and Shell Basics
by Emmett Dulaney


Regular expressions have set Unix-based operating systems apart from others almost since the very beginning. While other operating systems incorporate wild cards and quit there, Unix goes far beyond that. Regular expressions allow you to tweak and refine operations with tools ranging from directory searches to stream editing and everything in between. Not only do you need to know regular expressions, but you have to know some basics about the shell as well a few key commands in order to work with them. Following are 50 questions on regular expressions and shell basics at a knowledge level required for almost any intermediate-level certification exam. Good luck (answers are at the end of the article)!

1. Which of the following characters used to match any single character?

A. a dash (-)
B. an asterisk (*)
C. a question mark (?)
D. a period (.)

2. Which of the following characters is used to signify the beginning of a line?

A. %
B. $
C. ^
D. ~

3. You want to use the question mark character (?) in a regular expression search and not have it interpreted as a metacharacter. Which of the following should be used to accomplish this?

A. "\?"
B. "/?"
C. "$?"
D. "(?)"

4. How many occurrences of a character preceding an asterisk (*) must be present before it is considered a match?

A. zero
B. one
C. two
D. more than two

5. You want to find every line in a file that contains a number. Which of the following regular expressions will accomplish this?

A. -n
B. "0 1 2 3 4 5 6 7 8 9"
C. [0-9]
D. [^0-9]

6. You want to find all occurrences of "Muncie" in a file and suspect that sometimes it may appear initial-capped and other times all in lowercase. Which regular expression should you use to accomplish this?

A. [Muncie,muncie]
B. [Mm]uncie
C. -i muncie
D. {Muncie,muncie}

7. What regular expression should be used to find all lines that begin with a question mark? (Fill in the blank.)

8. Which character is used to mean that the preceding must be matched one or more times?

A. +
B. ?
C. &
D. @

9. You want to find all matches for two distinctly different values. Which character is used to signify that you want to match two values?

A. back slash (\)
B. forward slash (/)
C. pipe (|)
D. ampersand (&)

10.Which of the following should be used to find "Sam" and "Samantha" in a file?

A. Sam(antha)*
B. Sam[antaha]
C. Sam|antha
D. Sam\(antha\)\?

11. Which of the following can be used to find any white space, including spaces and tabs?

A. [white]
B. ""
C. [[:space:]]
D. [[tab|space]]

12. Which of the following characters is used to signify the end of a line?

A. %
B. $
C. ^
D. ~

13. What set of characters will be matched if the expression is [1-9A-D]?

A. 123456789ABCDabcd
B. 123456789ABCD
C. 19ADad
D. 19AD

14. Which of the following matches will be found to file\.html? (choose all that apply):
A. file.htmlll
B. file.html
C. file.htm
D. file

15. Which of the following regular expressions should be used to find lines that do not contain numbers?
A. -n
B. "\0 \1 \2 \3 \4 \5 \6 \7 \8 \9"
C. [0-9]
D. [^0-9]

16. On the command line, what does encompassing a string within single quotes accomplish?

A. It turns off the special significance of everything within the quotes.
B. It turns off the special significance of everything within the quotes except for a few very special characters.
C. It executes the command within.
D. It sets a variable value.

17. You want to perform a test on a string to make sure that the length of it is not zero before using it. Which test should be done to accomplish this?

A. [[ string ]]
B. [[ -z string ]]
C. [[ -n string ]]
D. [[ -a string ]]

18. You are writing a shell script that includes a number of choices within a case statement. What is used to end the case statement?

A. done
B. fi
C. end
D. esac

19. Which of the following can be checked to see how many positional parameters were used to call a script?

A. $#
B. $$
C. $!
D. $?

20. Which switch should be used with grep to print all lines in a file that do not match the search criteria given?

A. -i
B. -v
C. -c
D. -n

21. You are writing a simple shell script that will prompt the user and require them to enter a string variable. What command will you need to use within the script to input the user's string?

A. input
B. enter
C. read
D. record

22. What is the stream number associated with standard error?

A. 3
B. 2
C. 1
D. 0

23. Which of the following regular expressions would indicate that the search item preceding it must be found at least 5 times?

A. (5)
B. 5x
C. {5,}
D. [5+]

24. Which of the following should be used to test if two strings are equal?

A. string1 = string2
B. string1 –eq string2
C. string1 –a string2
D. string1 –o string2

25. Which of the following would be the result of the command echo 1{2,3,4}5 (choose all that apply)?

A. 15
B. 125
C. 135
D. 145
E. 12345
F. 12,3,45
G. 1{2,3,4}5

26. On the command line, what does encompassing a string within double quotes accomplish?

A. It turns off the special significance of everything within the quotes.
B. It turns off the special significance of everything within the quotes except for a few very special characters.
C. It executes the command within.
D. It sets a variable value.

27. Which of the following can be used to see the arguments given when the shell script was invoked?

A. $#
B. $?
C. $0
D. $*

28. You are including a while loop in a shell script. What is used to end the while loop?

A. done
B. fi
C. end
D. esac

29. What regular expression can be used to find all lines that end with a period? (Fill in the blank.)

30. Which switch should be used with grep to ignore case as you look for matches in a file?

A. -i
B. -v
C. -c
D. -n

31. Which of the following is a synonym for [^[:alnum:]] ?

A. \W
B. \w
C. \a
D. \A

32. Within the Bash shell, what is the tilde (~) translated to by default?

A. the PATH
B. the present working directory
C. the user's home directory
D. the TMP directory

33. With a simple command given at a prompt, how does Bash process it?

A. from right to left
B. from left to right
C. in alphabetic order
D. in ASCII order

34. Which of the following Bash built-in commands can be used within a shell script to parse positional variables?

A. shear
B. parse
C. trim
D. getopts

35. The typeset command exists in Bash for compatibility with Korn shell scripts. What command, however, is more favored for use with Bash?

A. slot
B. state
C. declare
D. proclaim

36. On the command line, what does encompassing a string within backticks accomplish?

A. It turns off the special significance of everything within the backticks.
B. It turns off the special significance of everything within the backticks except for a few very special characters.
C. It executes the command within.
D. It sets a variable value.

37. Which of the following can be used within a shell script to see the name with which the shell is being executed?

A. $#
B. $?
C. $0
D. $*

38. You are tweaking a shell script by adding an until loop. What needs to be used to end the until loop?

A. done
B. fi
C. end
D. esac

39. Which of the following should be used to test if the value of two integers is the same?

A. integer1 = integer2
B. integer1 –eq integer2
C. integer1 –a integer2
D. integer1 –o integer2

40. Which switch should be used with grep to count all lines in a file that match the search criteria?

A. -i
B. -v
C. -c
D. -n

41. What is the file descriptor/stream number for standard input? (Fill in the blank.)

42. Which of the following utilities processes most quickly?

A. grep
B. egrep
C. fgrep

43. Which of the following regular expressions means the search criteria preceding it must be found at least seven times, but not more than ten times?

A. {7,,10}
B. {7,10}
C. {7>10}
D. {7,!10}

44. Which of the following regular expressions can be used to find all blank lines in a file?

A.""
B. ^$
C. ^" "$
D. [ ]

45. In a shell script, you have nested one loop within another (both using the syntax for – do – done). What can be used to exit the first loop without exiting the second once a specific condition is met?

A. enod
B. end
C. break
D. rof

46. Which option can be used with test to see if a file exists and is a regular file?

A. -r
B. -e
C. -a
D. -f

47. What is the stream number associated with standard output?

A. 3
B. 2
C. 1
D. 0

48. Which of the following can be used to see the exit code of the last command or shell executed?

A. $#
B. $?
C. $0
D. $*

49. Which test on a string can be used to make sure that the length of it is zero?

A. [[ string ]]
B. [[ -z string ]]
C. [[ -n string ]]
D. [[ -a string ]]

50. If the value of ALT is set to the string "MPC", what will the command echo '$ALT' return?

A. $MPC
B. MPC
C. ALT
D. $ALT

Answers

1. A period (.) is used to match any single character. Answer: D.

2. The circumflex (^) is used to signify the beginning of a line. Answer: C.

3. You must use the backslash (\) with the metacharacter – "\?". Answer: A.

4. The asterisk (*) will match zero or more occurrences of the character(s) preceding it. Answer: A.

5. The expression [0-9] will find a match for any number between 0 and 9 (all numbers). Answer: C.

6. The expression [Mm]uncie will find both "Muncie" and "muncie" . Answer: B.

7. The regular expression to use is : ^\?

8. The plus sign (+) is used to indicate that the preceding must be matched one or more times. Answer: A.

9. A pipe (|) is used to separate multiple values being searched for. Answer: C.

10. Using the expression Sam\(antha\)\? will always match "Sam" and it can be followed by zero or one (?) occurrences of "antha" . Answer: D.

11. The expression [[:space:]] can be used to find any white space, including spaces and tabs. Answer: C.

12. The dollar sign ($) is used to signify the end of a line. Answer: B.

13. Anything within the character set 123456789ABCD will be matched the expression [1-9A-D]. Answer: B.

14. The question mark (?) will match zero or one occurrence of the "l", meaning that both file.htm and file.html will be found. Answer: B&C.

15. The expression [^0-9] will find a match to everything except the number set 0 to 9. Answer: D.

16. Single quotes are used to turn off the special significance of everything within the quotes. Answer: A.

17. The syntax [[ -n string ]] is used to see if the string has a length greater than zero. Answer: C.

18. The case statement ends with esac. Answer: D.

19. The value of $# can be checked to see how many positional parameters were used to call a script. Answer: A.

20. The –v switch is used to print all lines of a file that do not match the search criteria given. Answer: B.

21. The read command is used to accept a value and assign it to a variable. Answer: C.

22. Standard error is stream 2. Answer: B.

23. The expression {5,} means the preceding must be found at least five times. Answer: C.

24. To test and see if two string values are the same, the equal sign (=) is used. Answer: A.

25. The command will echo "1" and "5" with each number specified between the braces in the middle: 125, 135, and 145. Answer: B&C&D.

26. Double quotes turn off the special significance of everything within the quotes except for a few very special characters that remain (such as single quotes, the dollar sign, and back slash). Answer: B.

27. The variable $* can be used to see the arguments given when the shell script was invoked. Answer: D.

28. A while loop ends with done (while – do – done). Answer: A.

29. The regular expression to use is: \.$

30. The –i option is used to ignore case. Answer: A.

31. \W is a synonym for [^[:alnum:]]. Answer: A.

32. The tilde, by default, translates to the home directory of the user. Answer: C.

33. Simple commands are executed from left to right. Answer: B.

34. The command getopts is a Bash built-in that can be used within a shell script to parse positional variables. Answer: D.

35. The declare command is used to proclaim what type of variable it is so it can be handled accordingly. Answer: C.

36. A command can be placed within backticks and that command is then executed, with the value of execution becoming the resulting string/value. Answer: C.

37. The positional variable $0 holds the name with which the script was summoned. Answer: C.

38. An until loop ends with done (until – do – done). Answer: A.

39. A test can be done to see if the values of two integers are the same using the –eq parameter. Answer: B.

40. The –c switch is used to count lines that match the search criteria. Answer: C.

41. The file descriptor/stream number for standard input is 0.

42. The fgrep utility does not have the same regular expression set as the other two, and is thus able to execute the fastest. Answer: C.

43. The regular expressions {7,10} means the search criteria preceding it must be found at least seven times, but not more than ten times. Answer: B.

44. To find the blank lines, look for the beginning (^) and end ($) with nothing between them (^$). Answer: B.

45. The break statement can be used to exit a nested loop. Answer: C.

46. The –f option is used to see if a file exists and is a regular file. Answer: D.

47. Standard output is stream 1. Answer: C.

48. The value of $? is equal to the exit code returned by the last command executed. Answer: B.

49. The test [[ -z string ]] will verify that the length of the string is zero. Answer: B.50. Because the single quotes override the special meaning of the dollar sign,the string give ($ALT) is literally returned. Answer: D.

50. The original Ethernet standard, first published in 1980 by the consortium of DEC, Intel, and Xerox, specified a speed of 10 Mbps. Answer: C.

Emmett Dulaney is the author of the several books on Linux/Unix and certification as well as a columnist for UnixReview.com. Emmett's blog can be found at: http://edulaney.blogspot.com, and he can be reached (and welcomes your comments) at: edulaney@insightbb.com.

No comments: