โ† All 010-160 Flashcard Decks

Basic Scripting Concepts Flashcards

6 cards from real 010-160 practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Basic Scripting Concepts flashcards as text
  1. A shell script contains the following lines: MY_VAR="Linux System" echo "I am learning about the '$MY_VAR'" echo "I am learning about the \"$MY_VAR\"" What is the output when this script is executed?

    Answer: I am learning about the '$MY_VAR' I am learning about the "Linux System"

    Single quotes (') prevent any kind of expansion, so '$MY_VAR' is printed literally. Double quotes (") allow for variable expansion, so "$MY_VAR" is replaced with its value, "Linux System". The backslashes before the inner double quotes are necessary to escape them so they are printed as literal characters.

  2. Which of the following lines in a script correctly executes the `whoami` command and assigns its output to a variable named `CURRENT_USER`?

    Answer: CURRENT_USER=$(whoami)

    The `$(command)` syntax is used for command substitution. It executes the command within the parentheses (`whoami`) and substitutes its standard output. This output is then assigned to the `CURRENT_USER` variable.

  3. What is the primary purpose of the `#!/bin/bash` line at the beginning of a shell script?

    Answer: It specifies the absolute path to the interpreter that should execute the script.

    This line, known as a 'shebang', is an interpreter directive. It tells the operating system's program loader which program (in this case, `/bin/bash`) should be used to run the commands contained in the script.

  4. A user creates a script named `greet.sh` with the content `echo "Hello, $1"`. How should the user execute this script to produce the output `Hello, David`?

    Answer: ./greet.sh David

    Arguments passed to a script on the command line are known as positional parameters. `$1` refers to the first argument, `$2` to the second, and so on. By running `./greet.sh David`, the string "David" becomes the first argument and is substituted for `$1` within the script.

  5. After a command fails in a shell script, what is the most reliable way to check for this failure condition?

    Answer: Check if the output of the `$?` variable is a non-zero value.

    The special shell variable `$?` always holds the exit status of the most recently executed foreground command. By convention, an exit status of 0 indicates success, while any non-zero value indicates an error or failure.

  6. A user has written a script file named `run_report.sh` but receives a "Permission denied" error when trying to execute it with `./run_report.sh`. The output of `ls -l` shows the permissions as `-rw-r--r--`. Which command will resolve this issue?

    Answer: chmod +x run_report.sh

    To be executed directly, a script file requires the execute permission. The `chmod +x` command adds the execute (x) permission for the owner, group, and others, resolving the "Permission denied" error.