Error logging patterns in Bash Scripting - Time & Space Complexity
When writing error logs in bash scripts, it is important to understand how the time taken grows as the number of errors increases.
We want to know how the logging process scales when many errors happen.
Analyze the time complexity of the following code snippet.
errors=("file not found" "permission denied" "timeout" "disk full")
for err in "${errors[@]}"; do
echo "Error: $err" >> error.log
sleep 0.1
echo "Logged: $err"
done
This script loops over an array of error messages and writes each one to a log file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the error messages array and appending each error to the log file.
- How many times: Once for each error message in the array.
Each additional error message causes one more write to the log file.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes to the log file |
| 100 | 100 writes to the log file |
| 1000 | 1000 writes to the log file |
Pattern observation: The number of operations grows directly with the number of errors.
Time Complexity: O(n)
This means the time to log errors grows linearly as the number of errors increases.
[X] Wrong: "Logging multiple errors at once takes the same time as logging one error."
[OK] Correct: Each error requires a separate write operation, so more errors mean more time spent logging.
Understanding how error logging scales helps you write scripts that handle many errors efficiently and avoid slowdowns in real systems.
What if we buffered all error messages and wrote them to the log file in one go? How would the time complexity change?
Practice
2> operator do in a bash script?Solution
Step 1: Understand bash output streams
Bash has two main output streams: standard output (1) and standard error (2).Step 2: Identify the meaning of
The operator2>2>redirects the standard error stream to a file or location.Final Answer:
Redirects error messages to a file -> Option AQuick Check:
2>means error redirect [OK]
- Confusing 2> with > which redirects standard output
- Thinking 2> appends instead of overwriting
- Assuming 2> runs commands in background
error.log?Solution
Step 1: Understand redirection operators
2>redirects standard error and overwrites the file;2>>appends errors.Step 2: Match requirement to syntax
We want to redirect only errors and overwrite the file, so2> error.logis correct.Final Answer:
command 2> error.log -> Option DQuick Check:
Use 2> for error overwrite [OK]
- Using > which redirects standard output, not errors
- Using &> which redirects both output and errors
- Confusing append (>>) with overwrite (>)
script.sh prints "Success" to standard output and "Error occurred" to standard error?./script.sh > output.log 2> error.logSolution
Step 1: Analyze redirection operators
> output.logredirects standard output to output.log,2> error.logredirects standard error to error.log.Step 2: Match outputs to files
"Success" goes to standard output, so saved in output.log; "Error occurred" goes to standard error, saved in error.log.Final Answer:
"Success" is saved in output.log, "Error occurred" in error.log -> Option AQuick Check:
Separate output and error files [OK]
- Assuming both outputs go to same file
- Confusing 2> with >
- Expecting output on terminal despite redirection
errors.log:command 2>2 errors.logSolution
Step 1: Examine the redirection syntax
The correct syntax to redirect errors is2> filename. Here,2>2 errors.logredirects stderr to file2, treatingerrors.logas a command argument.Step 2: Identify the cause of error
The extra '2' makes the filename2instead oferrors.log.Final Answer:
Extra '2' causes errors to be redirected to file named '2' instead of 'errors.log' -> Option CQuick Check:
2> must be followed by correct filename [OK]
- Adding extra numbers or symbols in filename position
- Confusing &> with 2>
- Missing spaces causing parsing errors
Solution
Step 1: Understand output and error redirection
>overwrites standard output,2>>appends standard error to a file.Step 2: Match requirement to command
We want output overwritten in output.log and errors appended in error.log, so./script.sh > output.log 2>> error.logfits.Final Answer:
./script.sh > output.log 2>> error.log -> Option BQuick Check:
Use 2>> to append errors [OK]
- Using 2> which overwrites error file
- Using &> which merges output and errors
- Appending output but overwriting errors
Ready to prove it?
Pass the test and this pattern moves unknown
