LCA Shell Scripting & Automation — Questions and Answers
Question 1: Which symbol is used to start a comment line in a shell script?
- //
- # (Correct answer)
- --
- /*
Correct answer: #
In shell scripts (and many other scripting languages), the hash symbol (`#`) is used to denote a comment line. Any text following the `#` on that line will be ignored by the shell interpreter. Comments are essential for documenting code, explaining logic, and making scripts more understandable for others and for future self-reference.
Question 2: What command runs a shell script named 'myscript.sh'?
- execute myscript.sh
- run myscript.sh
- sh myscript.sh (Correct answer)
- start myscript.sh
Correct answer: sh myscript.sh
To run a shell script named `myscript.sh`, you can explicitly invoke the shell interpreter, such as `sh` (for Bourne shell compatible scripts) or `bash` (for Bash scripts), followed by the script's filename. This tells the system to execute the script using the specified shell. Alternatively, if the script has execute permissions and a shebang line, you can run it directly with `./myscript.sh`.
Question 3: Which command gives execute permission to a shell script?
- chmod +r
- chmod +x (Correct answer)
- chmod +w
- chmod 7777
Correct answer: chmod +x
The `chmod` command is used to change file permissions in Linux. The `+x` option specifically grants execute permission to a file, which is necessary for a shell script to be run directly as a program. Without execute permission, the script can only be read or modified, but not executed by simply typing its name.
Question 4: What does the 'echo' command do in a shell script?
- Reads input
- Prints output (Correct answer)
- Executes commands
- Terminates script
Correct answer: Prints output
The `echo` command is a fundamental utility in shell scripting used to display text or the value of variables to the standard output, typically the terminal. It's commonly used for printing messages, debugging, or showing results within a script. For example, `echo "Hello World"` would print "Hello World".
Question 5: Which variable holds the name of the currently running script?
- $1
- $@
- $0 (Correct answer)
- $#
Correct answer: $0
In shell scripting, `$0` is a special variable that holds the name of the script itself as it was invoked. This is useful for various purposes, such as printing usage messages or determining the script's identity. Other variables like `$1`, `$@`, and `$#` refer to command-line arguments or their count.
Question 6: How do you declare a variable in a shell script?
- var = value
- VAR = value
- VAR=value (Correct answer)
- set VAR value
Correct answer: VAR=value
To declare and assign a value to a variable in a shell script, you use the syntax `VAR=value` without any spaces around the equals sign. This assigns the string 'value' to the variable named 'VAR'. Spaces would cause the shell to interpret `VAR` as a command and `=` as its argument, leading to an error.
Question 7: What is the output of 'echo $((3 + 2))'?
- 3 + 2
- 5 (Correct answer)
- $((3 + 2))
- error
Correct answer: 5
The `$((...))` construct in shell scripting performs arithmetic expansion. It evaluates the arithmetic expression enclosed within the double parentheses and returns the numerical result. In this case, `3 + 2` evaluates to `5`, which `echo` then prints to the standard output.
Question 8: How do you execute a command stored in a variable?
- execute $VAR
- run $VAR
- eval $VAR (Correct answer)
- call $VAR
Correct answer: eval $VAR
The `eval` command is used to evaluate its arguments as a shell command. If a variable, say `VAR`, contains a string that represents a command, `eval $VAR` will execute that string as if it were typed directly into the shell. This is powerful for dynamic command execution but should be used carefully due to potential security risks.
Question 9: Which loop is used to iterate over a list of items in shell scripting?
- while
- for (Correct answer)
- until
- do-while
Correct answer: for
The `for` loop is specifically designed for iterating over a list of items, such as words, files, or numbers, one by one. It executes a block of commands for each item in the specified list. In contrast, `while` and `until` loops execute based on a condition being true or false, respectively.
Which symbol is used to start a comment line in a shell script?