Please answer the quiz and click the "Test" button at the bottom right.This quiz is part of the DevOpsTheHardWay course.
Bash - loops - multichoice questions
Given the following script, answer the following 3 questions:
[elvis@station elvis]$ cat script
#!/bin/bash
for i in $*
do
if [ -r $i ]
then
gzip $i
else
echo "cannot compress $i"
done
fi
[elvis@station elvis]$ ./script rotate_cw
./script: line 10: syntax error near unexpected token `done'
./script: line 10: `
done'
Question 1
Which of the following lines could replace the line if [ -r $i ]
above, with no effect on script execution?
-
if test -r $i
-
if [-r $i]
-
if [ -r -o $i ]
-
if [ -e $i
- None of the above
Question 2
What syntax error exists in the script?
- The words
for
anddo
must occur on the same line. - There must be no spaces between
[
and-r
on the line startingif
. - The
gzip
command must be specified using an absolute reference. - The last line contains the misspelled word
fi
. - The last two lines (containing
done
andfi
) need to be transposed.
Question 3
What does the variable i
iterate through (assuming the syntax error mentioned above is fixed)?
- All (non-hidden) files in the local directory.
- All files in the local directory.
- All files which were previously defined in the environment variable named
*
. - All of the command line arguments provided when the script was invoked.
- None of the above.
Question 4
The following text is found in the file /etc/bashrc
.
if [ "$(id -gn)" = "$(id -un)" -a "$(id -u)" -gt "99" ]; then
umask 002
else
umask 022
fi
Which of the following best describes the execution of this text?
- If the current user is a member of more than one group, then set the current shell's umask to 002, otherwise set it to 022.
- If the current user's group name is the same as the user's username, or the user has a userid greater than 99, set the shell's umask to 002, otherwise set it to 022.
- If the user is a member of more than 99 groups, set the shell's umask to 002, otherwise set it to 022.
- If the current user is the root user, set current shell's umask to 002, otherwise set it to 022.
- If the current user's group name is the same as the user's username, and the user has a userid greater than 99, set the shell's umask to 002, otherwise set it to 022.
Question 5
The following text is found in the file /etc/profile
.
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
Which of the following best describes the execution of this text?
- For every file in the
/etc/profile.d
directory that ends.sh
, if it is readable, source it. - For every file in the
/etc/profile.d
directory that ends.sh
, if it is executable, source it. - For every file in the
/etc/profile.d
directory that ends.sh
, if it is not a directory, source it. - None of the above.
Question 6
The following text is found in the file /etc/profile
.
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
What innocent action could a system administrator make that causes an error (exit status other than 0) when this section of the above script is executed?
- The administrator could place a file that does not end with
.sh
in the/etc/profile.d
directory. - The administrator could place a file that does not have executable permissions in the
/etc/profile.d
directory. - The administrator could place a file that does not have read permissions in the
/etc/profile.d
directory. - The administrator could place a file that does not have write permissions in the
/etc/profile.d
directory. - The administrator could place a file titled source
me.sh
in the/etc/profile.d
directory. - None of the above