Shell Scripting Fundamentals Quiz Quiz

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.

  1. Variable Assignment

    Which is the correct way to assign the string 'hello' to a variable named 'greet' without introducing unwanted spaces in shell scripting?

    1. greet = 'hello'
    2. greet == 'hello'
    3. set greet 'hello'
    4. greet='hello'

    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.

  2. Executing a Script

    If a script called 'backup.sh' is made executable, which command will correctly run it from its current directory?

    1. bash ./backup.sh/
    2. sh backup.sh/
    3. ./backup.sh
    4. /backup.sh

    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.

  3. Conditional Statements

    In shell scripts, which syntax correctly checks if a file named 'report.txt' exists?

    1. [ -e report.txt ]
    2. [ -f report.txt ]
    3. [ -d report.txt ]
    4. [ -z report.txt ]

    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.

  4. Loop Structures

    Which of the following demonstrates the correct use of a 'for' loop to print numbers 1 to 3 in a shell script?

    1. for $i in 1,2,3 { print $i }
    2. for (i=1; iu003C=3; i++); echo $i
    3. loop i=1 to 3; echo $i; end
    4. for i in 1 2 3; do echo $i; done

    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.

  5. File Permissions

    What does the command 'chmod u+x script.sh' accomplish in shell scripting?

    1. It sets read-only permission for the owner.
    2. It adds execute permission for all users.
    3. It grants execute permission to the file owner for 'script.sh'.
    4. It removes write permission from the file owner.

    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.