None of the three methods will expand to the user's home directory. Only using "$HOME/photo dir"will be successful.
Only method 1 will expand "~/" to the user's home directory and then append the quoted directory name that includes a space.
Only method 2 will expand "~/" to the user's home directory and then append the quoted directory name that includes a space.
Only method 3 will expand "~/" to the user's home directory and then append the quoted directory name that includes a space.
Q2. If script.sh is run in the current directory, it will fail. Why?
$ ls -1
Beach photo1.jpg
Photo1.jpg
Photo2.jpg
Script.sh
$ cat script.sh
for i in $(ls *.jpg); do mv $i${i}.bak
done
ls: cannot access nonexistentfile: No such file or directory
The for loop will split on word boundaries and Beach photo1.jpg has a space in it.
The mv command will fail because the curly bracket is a special character in Bash and cannot be used in the names of files.
Running script.sh will be successful as the ls command builds a list of files in the current directory and for loops through that list renaming files with a .bak extension.
Q3. To run a copy command in a subshell, which syntax would you use?
( command )
sh command
{ command; }
(( command ))
reference. Subshells are one way for a programmer to capture (usually with the intent of processing) the output from a program or script. Commands to be run inside a subshell are enclosed inside single parentheses and preceeded by a dollar sign: DIRCONTENTS=$(ls -l) echo ${DIRCONTENTS}
Q4. Using "awk", what would the output of this command string be?
reference. AWK is a programming language that is designed for processing text-based data, either in files or data streams, or using shell pipes. In other words you can combine awk with shell scripts or directly use at a shell prompt.
Q5. The command below will search the root filesystem for files named "finance.db". In this context, what information is being sent to /dev/null?
information sent to the standard error-for example, errors that the find command displays as it runs
the names of files that match finance.db
information sent to the standard output-that is, the path to files the find command has located
reference. Syntax to redirect stderr (standard error) to a file: command 2> errors.txt.
Q6. To permanently remove empty lines from a file called textfile, which command could you use?
sed -i '/^$/d' textfile
sed '/^$/d' textfile
cat textfile | sed '/^$/d
sed -i 's/^$//' textfile
reference
sed : sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream.
-i[SUFFIX] : This option specifies that files are to be edited in-place.
'/^$/d' : regex is between the //. ^ is the beginning of the line, $ is the end of the line. ^$ means the start and end have nothing in between.
d : Delete the pattern space; immediately start next cycle.
Warning, this example above will not work on a mac terminal due to different UNIX flavours. There is a way to make it work on a mac adding an extra flag -e, or even just -- (found on StackOverflow): sed -i -e '/^$/d' textfile.txt
Q7. Assuming that user1 existed, what would be the result of this command string?
It would show the username, UID, and home directory of user1 separated by colons.
It would print the UID, GID, and home directory of user1 separated by hyphens.
It would print the UID, comment, and home directory of user1 separated by hyphens.
It would show the username, UID, and home directory of user1 separated by hyphens.
reference. Traditionally, the /etc/passwd file is used to keep track of every registered user that has access to a system. The /etc/passwd file is a colon-separated file that contains the following information: 1-Username, 2-Password, 3-User ID (UID), 4-Group ID (GID), 5-User ID Info (GECOS), 6-Home directory, 7-Command/shell
Q8. What happens if you use the "set -e" in a Bash script?
It will cause Bash to exit if a function or subshell returns a nonzero status code.
It will cause Bash to exit if a conditional returns a non-zero status code.
It will cause Bash to exit if local, declare, or typeset assignments return a nonzero status code.
It will cause Bash to exit if a command, list of commands, compound command, or potentially a pipeline returns a nonzero status code.
reference. The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful. In all widely used general-purpose programming languages, an unhandled runtime error - whether that's a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.
Q9. The _ keyword pauses the script to get input from standard input.
get
argument
read
input
Q10. If file.sql holds SQL statements to be executed, what will be in file.txt?
mysql < file.sql > file.txt
a copy of the contents of file.sql
an error indicating that this is invalid syntax
the error output of the MySQL command
the non-error output of the MySQL command
Note: check the question below for a variant.
Q11. What will be the difference between the output on the screen and the contents of out.txt
mysql < file.sql > out.txt
The output on the screen will be identical to out.txt
There will be no output on the screen as it's being redirected to out.txt.
The output on the screen will be identical to out.txt plus line numbers.
The out.txt file will hold STDERR and STDOUT will go to the screen.
Note: check the question above for a variant.
Q12. How does the SUID or setuid affect executable commands?
When the command creates files, they will be owned by the group owner of the command.
The SUID bit allows anyone to execute the command no matter what other permissions are set.
When the command is executed, its running privileges elevate to the user owner of the command.
When the command is executed, its running privileges elevate to the group owner of the command.
reference. The Linux and Unix access rights flags setuid and setgid (short for set user identity and set group identity)[1] allow users to run an executable with the file system permissions of the executable's owner or group respectively and to change behaviour in directories.
Q13. In order to extract text from the first column of file called textfile, which command would you use?
cat {$1,textfile}
cat textfile | awk [print $1]
cat textfile | awk '{print $1}'
awk textfile {print $1}
Q14. What is the keyboard shortcut to call up the Bash history search as shown below?
(reverse-i-search)`':
Esc + R
Ctrl + H
Ctrl + R
Alt + R
Note: On the Mac it will show bck-i-search: instead of (reverse-i-search).
Q15. Which arithmetic expression will give the most precise answer?
var=$( expr 10 / 8 )
(( var= 10 /8 ))
var=$(( 10 / 8 ))
var=$(echo 'scale=2; 10 / 8' | bc)
reference. The bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. The division with 2 digit precision will be passed to bc, evaluated, and assigned to the variable.
Q16. What is the result of this script?
txt=Penguins
[[ $txt =~ [a-z]{8} ]]; echo $?
0, representing 'true', because the variable "txt" contains eight letters
0, representing 'true', because everybody loves penguins!
1, representing 'false', because the variable "txt" is longer than eight characters
1, representing 'false', because the variable "txt" does not contain eight lowercase letters between a and z
Q17. How would you change your Bash shell prompt to the following?
reference
What is happening here quoting the POSIX shell specification: ${parameter#[word]}. Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted.
For instance ${VAR#?} expands to the value of $VAR with the first character deleted. And ${VAR#\*/html}expands to include all characters to and including the/htmltext which will be deleted from the variable producing the output of/website.com/html/
Q19. If prompted for text at the standard input, you can tell the command you're done entering text with what key combination?
Ctrl + A (Windows) or Command + A (Mac)
Ctrl + E (Windows) or Command + E (Mac)
Ctrl + D (Windows) or Command + D (Mac)
Ctrl + Z (Windows) or Command + Z (Mac)
Q20. In order for a Bash script to be executed like an OS command, it should start with a shebang line. What does this look like?
#!/usr/bin/env bash
~/usr/bin/env bash
'$!/usr/bin/env bash
#/usr/bin/env bash
Q21. What line of Bash script probably produced the output shown below?
The date is: Sun Mar 24 12:30:06 CST 2019!
echo "The date is: !"
echo "The date is: date!"
echo "The date is: (date)!"
echo "The date is: $(date)!"
Q22. Suppose your current working directory is your home directory. How could you run the script demo.sh that is located in your home directory? Find three correct answers.
A. /home/demo.sh
B. ./demo.sh
C. ~/demo.sh
D. bash /home/demo.sh
E. bash demo.sh
B, C, E
A, B, C
C, D, E
B, D, E
Q23. How could you get a list of all .html files in your tree?
find . -type html
find . -name *.html
find *.html
find . -name \*.html -print
The second seems well, but will expand the \* if there is any .html file on your working directory.
Q24. What would be in out.txt?
cat < in.txt > out.txt
The output from the command line. By default STDIN comes from the keyboard.
Nothing because you can't redirect from file (in.txt) to another file (out.txt). You can only redirect from a command to a file.
It would be the contents of in.txt.
Nothing. The redirect will create a new empty file but there will not be any output from the cat command to redirect.
Q25. What does this bash statement do?
(( $a == $b ))
echo $?
It loops between the values of $a and $b.
It tests whether the values of variables $a and $b are equal.
It returns $b if it is larger than $a.
It returns $a if it is larger than $b.
Q26. What do you use in a case statement to tell Bash that you're done with a specific test?
; ;
: :
done
$$
Q27. What does the asterisk represent in this statement?
Q37. Which file allows you to save modifications to the shell environment across sessions?
/etc/bash.conf
~/.profile
/etc/bashprofile
~/profile
Q38. Given the listed permissions on data.txt is it possible that user2 could have read, write, and execute permissions on data.txt?
$ ls -l
total 0
-rwx------+ 1 user1 user1 0 Oct 27 10:54 data.txt
No, it's clear that user2 does not have read, write, and execute permissions.
Yes, the + at the end of the 10-digit permission string signifies there's an access control list. This could possibly give user2 permissions not visible by ls -l.
It's possible that SELinux provides read, write, and execute permissions for user2 which are not visible with ls -l.
Yes, the + at the end of the 10-digit permission string signifies there's an extended attribute set. This could give user2 permissions to read, write, and execute data.txt.
Q39. What does this script accomplish?
#!/bin/bashdeclare -A ARRAY=([user1]=bob [user2]=ted [user3]=sally)
KEYS=(${!ARRAY[@]})
for (( i=0; $i < ${#ARRAY[@]}; i+=1 ));doecho${KEYS[$i]} - ${ARRAY[${KEYS[$i]}]}done
It sorts the associative array named ARRAY and stores the results in an indexed array named KEYS. It then uses this sorted array to loop through the associative array ARRAY.
Using a C-style for loop, it loops through the associative array named ARRAY using the associative array's keys and outputs both the key and values for each item.
It creates an indexed array of the associative array named ARRAY. It then uses a C-style for loop and the indexed array to loop through all items in the associative array, outputting the key and value of each array item using the index number.
It creates an associative array named ARRAY, which it loops through using a C-style for loop and the index numbers of each item in the associative array's keys, outputting the value of each item.
Q40. What file would match the code below?
ls Hello[[.vertical-line.]]World
Nothing, this is an invalid file glob.
Hello.vertical-line.World
Hello[[.vertical-line.]]World
Hello|World
Q41. What will be in out.txt?
ls nonexistentfile | grep "No such file" > out.txt
No such file
ls: cannot access nonexistentfile: No such file or directory
Nothing, out.txt will be empty.
It will be the contents of nonexistentfile.
Q42. For the script to print "Is numeric" on screen, what would the user have to enter when prompted?
#!/bin/bashread -p "Enter text " var
if [[ "$var" =~ "^[0-9]+$" ]];thenecho"Is numeric"elseecho"Is not numeric"fi
Any sequence of characters that includes an integer
The user would have to enter the character sequence of ^[0-9]]+$ Only this will prove to be true and "Is numeric" would be printed on the screen due to incorrect syntax. By encapsulating the regular expression in double quotes every match will fail except the text string ^[0-9]+$
One or more characters that only includes integers
Due to a syntax error it is impossible to get the script to print "Is numeric"
The regex must not be quoted to work properly.
Q43. How would you find the last copy command run in your history?
history | find cp
history | grep cp
grep cp history
cp history
Q44. In order to write a script that iterates through the files in a directory, which of the following could you use?
for i in $(ls); do ... done
for $(ls); do ... done
for i in $ls; do ... done
for $ls; do ... done
Q45. When executing a command and passing the output of that command to another command, which character allows you to chain these commands together?
Q57. The code below seems to work and outputs "8 is greater than 5". However, what unexpected result will tell you it is not functioning properly?
#!/bin/bashvar="8"if [ $var > 5 ]; thenecho"$var is greater than 5"fi
There will be no unexpected results. This script works as is and the output will be "8 is greater than 5".
The comparison will not be able to handle floating-point numbers, as Bash only handles integers. So this example will output an error message if the value of $var is changed to "8.8".
There will be a file in the current directory named 5.
The variable $var is not quoted, which will lead to word splitting. This script will fail with a "unary operator expected" message if you change the value of
Q58. What is the result of this script?
It removes the directory 'foo' and the files contained within it.
It removes all files except those in the current directory.
It removes all files in the current directory.
It removes all files except those in the 'foo' directory.
Q59. Which one is true?
SELinux policy rules are checked after DAC rules.
SELinux policy rules are checked before DAC rules
SELinux policy rules are never checked after DAC rules.
It creates an empty file called "notes" and then exits.
It outputs the contents of the "notes" file to the screen, and then deletes it.
It accepts text from the standard input and places it in the "notes" file.
It appends text to an existing file called "notes."
Q82. You want to match five-letter palindromes such as radar, rotor, and tenet. Which sed option should you use?
sed -E -n '/^(.)(.)\3\2\1$/p'
sed -E -n '/^(.)(.)(.).\2\1$/p'
sed -E -n '/^(.)(.)(.)\2\1$/p'
sed -E -n '/^(.)(.)(.)(.)\3\2\1$/p'
Q83. To add a value to the current environment, what command should you use ?
shell_add
save
echo
export
Q84. What is the difference between these two conditional expressions?
[[$A==$B]]
[[$A -eq $B]]
[[$A == $B]] is used for numeric comparisons whereas [[$a-eq $B]] is used for text comparisons.
[[$A==$B]]is the new way of doing comparison where [[$a-eq $B]]is the legacy syntax.
they are the same.
[[$A==$B]]is used for text comparisons whereas [[$a-eq $B]]is used for numeric comparisons.
Q85. What is the output of this code?
VAR="united states"echo"${VAR^}"
unitedstates
United States
United states
UNITED STATES
Q86. What would happen if you ran the script below as it is written?
#!/bin/bash#condition 1if [ $foo = "bar" ]; thenecho"foo is bar"fi#condition 2if [[ $foo = "bar" ]]; thenecho"foo is bar"fi
Both conditions will fail.
Both conditions will succeed.
Condition 1 would succeed and Condition 2 would fail.
Condition 1 would fail and Condition 2 would succeed.
Explanation: The script as written outputs line 3: [: =: unary operator expected. Define variable and assign value foo="bar", and both conditions will succeed.
Q87. Which variable contains the number of arguments passed to a script from the command line?
$#
$@
0
$!
Q88. In Bash scripting, what does the "shebang" (#!) at the beginning of a script indicate, and why is it important?
It indicates the location of the Bash interpreter that should be used to execute the script.
It specifies the version of Bash required to run the script.
It marks the script as executable.
It helps the system identify the script's interpreter, ensuring the correct interpreter is used.
Q89. Which variable contains the process ID (PID) of the script while it’s running?
$ID
$@
$#
$$
Q90. If a user wants to execute script sh without a shebang fine or execute permissions, what should the user type?
%OPTION% A shebang line is required to execute a shell script.
%OPTION% 'bash script.sh'.
%OPTION% 'exec script.sh'.
%OPTION% ExecuteExecute permissions are required to execute a shell script.
Q91. Which choice is the most likely output of the compound command shown below?
cat -n animals | sort -r | head -n 5
a.
1 Ant
2 Bear
3 Cat
4 Dog
5 Elephant
b.
9 Ibex
B Hippo
7 Giraffe
6 Fox
5 Elephant
4 Dog
3 Cat
2 Bear
1 Ant10 Jaguar
c.
Jaguar
Ibex
Hippo
Giraffe
Fox
d.
9 Ibex
8 Hippo
7 Giraffe
6 Fox
5 Elephant
Q92. Which of the following is not a valid Bash variable name?
$HOME
my_var
1var
!
Q93.In Bash, create a one-liner command that recursively finds all files with the ".txt" extension in a directory and its subdirectories, and counts the total number of lines in those files. The output should only display the total line count.
Which of the following one-liner Bash commands accomplishes this task?