Chapter 19. An Introduction to Bash Shell Scripting Flashcards
What is the effect if a script does not start with a shebang?
The script will be interpreted by the same shell as the parent shell.
How can you check if a variable VAR has no value?
test -z $VAR or [ -z $VAR ] can be used to check whether a variable VAR has no value.
What would you use in a script to count the number of arguments that have been used?
Use $# to count the number of arguments that have been used.
What would you use to refer to all arguments that have been used when starting the script?
Use $@ to refer to all arguments that have been used when starting the script.
How do you process user input in a script?
Use “read SOMEVAR” to process user input in a script.
What is the simplest way to test whether a file exists and execute the command echo “file does not exist” if it does not?
”[ -f filename ] || echo file does not exist” determines whether the file exists and, if not, executes the specified command.
Which test would you perform to find out if an item is a file or a directory?
[ -e filename ] can be used to determine whether an item is a file or a directory.
Which construction would you use to evaluate a range of items?
A for statement is typically used to evaluate a range of items.
How do you close an elif statement in a script?
You do not; it is a part of the if statement that is closed with fi.
In a case statement, you evaluate a range of items. For each of these items you execute one or more commands. What do you need to use after the last command to close the specific item?
Using ;; after the last command closes the specific item.