Explore key concepts of shell scripting with focused questions on variables, control structures, file permissions, and script execution. This quiz is designed to strengthen your understanding of scripting syntax, best practices, and essential shell commands for automation.
Which is the correct way to assign the string 'hello' to a variable named 'greet' without introducing unwanted spaces in shell scripting?
Explanation: The correct syntax in shell scripting uses 'greet='hello'' with no spaces around the equal sign. Including spaces, as in 'greet = 'hello'', is incorrect and results in an error. Using 'greet == 'hello'' is not valid for assignment; it is generally used for comparison. 'set greet 'hello'' is also not standard for variable assignment in shell scripting.
If a script called 'backup.sh' is made executable, which command will correctly run it from its current directory?
Explanation: Executing './backup.sh' runs the script located in the present directory because the dot-slash explicitly points to the current folder. Using '/backup.sh' refers to the root directory, which is unlikely to contain your script unless moved there. 'bash ./backup.sh/' and 'sh backup.sh/' use extra slashes at the end, which is an invalid path since 'backup.sh' is a file, not a directory.
In shell scripts, which syntax correctly checks if a file named 'report.txt' exists?
Explanation: '[ -f report.txt ]' checks specifically if a file exists and is a regular file. '[ -d report.txt ]' checks for a directory, not a file. '[ -e report.txt ]' checks for the existence of any type of file, but does not specifically verify it as a regular file. '[ -z report.txt ]' is used to check if a string variable is empty, not for file presence.
Which of the following demonstrates the correct use of a 'for' loop to print numbers 1 to 3 in a shell script?
Explanation: The correct shell scripting syntax for a 'for' loop is 'for i in 1 2 3; do echo $i; done', which loops over each value and echoes it. 'for (i=1; iu003C=3; i++); echo $i' follows another shell's (or language's) C-style syntax, which does not work in standard shell. 'loop i=1 to 3; echo $i; end' is not valid in shell scripting. 'for $i in 1,2,3 { print $i }' uses incorrect variable notation and control structure.
What does the command 'chmod u+x script.sh' accomplish in shell scripting?
Explanation: 'chmod u+x script.sh' specifically gives execute permission to the user (owner) of the file. It does not remove write permission; it only modifies execute. The command does not add permissions for all users—'u' targets just the file's owner. It also does not set the file to read-only for the owner, as it only handles the execute flag.