NDX Essentials Chapter 10/11
case
A conditional that lets you make multiple comparisons with a pattern is called: fanout case if branch test
Running the script will invoke /bin/csh to interpret the rest of the file
A file begins with #!/bin/csh. This means: This is a Perl script The operator should not be using /bin/csh C Shell compatibility mode is enabled Running the script will invoke /bin/csh to interpret the rest of the file Nothing, this is a comment
...send the output of one command to another.
A pipe allows you to... ...send the same input to multiple commands. ...type multiple commands at one prompt. ...send the output of one command to another. ...send the output of a command to a file.
True
A successful command may, or may not print output to STDOUT.True or False? True False
STDERR
Channel 2 is: STDOUT STDALL STDERR STDIN
STDERR
Error messages generated by commands are sent where by default? STDERR STDIN Log files STDOUT
It is the first argument passed to the script
Given the following part of a script: if [ -f $1 ]; then echo "I am here" fi What is the meaning of $1? It is a parameter to -f, indicating the size of the file It is the first argument passed to the script It is a special variable that indicates the exit code of the command before it It is a file called $1 It is a list of files that gets interpolated
If a file called "goodbye" exists in the current directory
Given the following script that is run through ./test.sh hello goodbye: if [ -f $2 ]; then echo "I am here" fi When will "I am here" be printed? If a file called "goodbye" exists in the current directory Never The script will always print "I am here" If there are two files in the current directory If a file called "hello" exists in the current directory
If a file called /tmp/foo exists, process_data won't be run process_data will be called at most once
Given the following script: while [ ! -f /tmp/foo ]; do echo -n "." process_data > /tmp/foo done Which of the following are true? (choose two) The screen will fill with dots. If a file called /tmp/foo exists, process_data won't be run process_data will be called at most once process_data will never be run /tmp/foo will be removed if it exists
exit 42
How would you finish your script with an exit code of 42? return 42 break 42 CODE=42 exit 42 $?=42
test -d /tmp/foo -o $USERS -gt 5
How would you write a test that says "if /tmp/foo is a directory or USERS is greater than 5"? test -d /tmp/foo -o $USERS -gt 5 test -d /tmp/foo | $USERS > 5 test /tmp/foo || $USERS > 5 test /tmp/foo -d -o $USERS -gt 5
Control and another character
Most of nano's commands take the form of: Mouse clicks Escape followed by another character Control and another character The F1 through F12 function keys Alt and another character
true
The command echo "text" > file.txt will create file.txt if it does not already exist. True or False? True False
False
The command echo "text" > file.txt will not overwrite file.txt if it already exists. True or False? True False
true
The command echo "text" >> file.txt will not overwrite file.txt if it already exists. True or False? True False
True
The grep command can be used with glob characters. True or False? True False
...will display all the lines in a file containing the specified Regular Expression.
The grep command... ...will display all the lines that begin with the specified Regular Expression. ...will display all the lines in a file containing the specified Regular Expression. ...will display the line numbers in a file that contain a specified Regular Expression. ...is not case sensitive.
0
The if command looks for what exit code to consider a condition to be true? 1 0 2 10 255
test $USERS -eq 5
The number of users logged in is in a variable called USERS. How would you test to see if 5 users are logged in? test $USERS -eq 5 test -f USERS=5 test $USERS = 5 test $USERS,5 test $USERS -a 5
Creates /tmp/foo if it does not exist
What does this shell script do? FOO=/tmp/foo if [ ! -d $FOO ]; then mkdir $FOO fi Creates /tmp/foo if it does not exist Outputs a message to the screen Creates /tmp/foo and raises an error if there is a problem Nothing, since there is a problem with the conditions in the if statement Makes the /tmp/foo directory if a file by that name exists
The previous command's exit code
What information is held inside $? ? The name of the command run The previous command's exit code The number of arguments passed to the script The current process id The current user ID
A="Hello"
What is the correct way to assign the word "Hello" to a variable? A = "Hello" echo "Hello" > A echo $A "Hello" $A="Hello" A="Hello"
A=`pwd`
What is the correct way to save the current directory to a variable? A=`pwd` pwd | $A A=cwd A=pwd pwd $A
1 will be added to the i variable
What is the meaning of $(( $i + 1)) ? 1 will be added to the i variable If i is 0, the loop will stop This will return the value of the next argument to the script This will return the value of the first argument to the script This runs the command stored in variable i
vi nano
Which are appropriate editors for writing shell scripts?(choose two) Firefox /bin/bash LibreOffice Writer vi nano
nl
Which command can be used to print line numbers? num nl ln sort
sort < list.file cat list.file | sort
Which command(s) can be used to sort the lines of list.file alphabetically and display it on the screen? (choose two) sort < list.file cat list.file >> sort echo list.file > sort cat list.file | sort
for loops operate over a fixed list of items while loops have a test each cycle to determine if it should run again
Which of the following are correct about for and while loops? (choose two) for loops have a test each cycle to determine if it should run again for loops operate over a fixed list of items for loops require a variable over which to iterate while loops operate over a fixed list of items while loops have a test each cycle to determine if it should run again
more less
Which of the following commands can be used to scroll through a text file? (choose two) some more cat less
echo Testing >> output.file
Which of the following commands will append its output to output.file? echo Testing >> output.file echo Testing > output.file output.file < echo Testing echo Testing -> output.file
ls /root 2> error.log
Which of the following commands will direct error messages to the file, error.log? ls /root >> error.log ls /root > error.log ls /root 2> error.log ls /root $> error.log
egrep 'start|end' file.txt
Which of the following commands will display lines that contain either start or end? egrep 'start|end' file.txt egrep (start|end) file.txt egrep start end file.txt egrep start&end file.txt
grep ^start file.txt
Which of the following commands will display only lines that begin with start? grep \start file.txt grep $start file.txt grep *start file.txt grep ^start file.txt
grep ^test file.txt
Which of the following commands will display only lines that begin with test? grep $test* file.txt grep *test file.txt grep &test file.txt grep ^test file.txt
-d
Which option for the cut command is used to specify a delimiter? -D -f = -d
-f
Which option for the cut command is used to specify the field? -d # -D -f
-l
Which option for the wc command will print the number of lines in a file? -w -l -L -C
-w
Which option for the wc command will print the total number of words in a file? -l -C -L -w
-n 5
Which option of the head command will display only the first five lines of a file? No option needed; head displays only five lines by default. -n -l 5 -n 5
read
Which shell command accepts input from the user's keyboard? echo read gets input $1