0
0
Bash Scriptingscripting~5 mins

Configuration file reading in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a configuration file in scripting?
A configuration file stores settings or options that a script can read to change its behavior without changing the script code.
Click to reveal answer
beginner
How do you read a simple key-value pair from a configuration file in Bash?
You can use the 'source' command to load the file if it contains valid Bash syntax, or use tools like 'grep' and 'cut' to extract values.
Click to reveal answer
beginner
What does the command 'source config.cfg' do in a Bash script?
It runs the commands inside 'config.cfg' in the current shell, making variables and functions defined there available to the script.
Click to reveal answer
intermediate
Why should configuration files avoid complex commands when used with 'source'?
Because 'source' executes the file as code, complex commands can cause errors or unexpected behavior. Configuration files should only set variables.
Click to reveal answer
intermediate
How can you safely read a configuration file with comments and blank lines in Bash?
Use 'grep' to ignore comments and blank lines, then parse key-value pairs with 'cut' or 'awk' to extract needed values.
Click to reveal answer
Which Bash command loads variables from a configuration file into the current script?
Acat
Bsource
Cecho
Dgrep
What is the best format for a configuration file to be sourced by a Bash script?
APlain text with key=value pairs
BBinary file
CJSON format
DXML format
How can you ignore comment lines starting with '#' when reading a config file with grep?
Agrep -i 'comment' config.cfg
Bgrep '^#' config.cfg
Cgrep -v '^#' config.cfg
Dgrep -c '#' config.cfg
If a config file has 'PORT=8080', how do you access this value after sourcing it?
Aecho PORT
BPORT()
Cget PORT
D$PORT
What happens if a config file sourced by Bash contains commands that delete files?
AThose commands will run and may delete files
BThey will be ignored
CAn error will occur
DThe script will skip those lines
Explain how to safely read and use a configuration file in a Bash script.
Think about how the script gets settings without errors.
You got /4 concepts.
    Describe the risks of sourcing an untrusted configuration file in Bash.
    Consider what happens when code runs unexpectedly.
    You got /4 concepts.