Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why patterns solve common automation needs in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Automation Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a loop pattern in Bash
What is the output of this Bash script?
Bash Scripting
for i in {1..3}; do echo "Step $i"; done
A
1
2
3
B
Step 1
Step 2
Step 3
CStep 1 Step 2 Step 3
DStep 3
Attempts:
2 left
💡 Hint
Look at how the loop prints each step on a new line.
🧠 Conceptual
intermediate
2:00remaining
Why use patterns in automation scripts?
Which reason best explains why using common patterns helps in automation scripting?
APatterns reduce errors by reusing tested solutions.
BPatterns prevent scripts from running on weekends.
CPatterns make scripts run slower but look nicer.
DPatterns increase the number of manual steps needed.
Attempts:
2 left
💡 Hint
Think about how reusing solutions affects reliability.
📝 Syntax
advanced
2:00remaining
Identify the correct Bash pattern for checking file existence
Which option correctly checks if a file named 'data.txt' exists in Bash?
Aif -f data.txt; then echo "File exists"; fi
Bif ( -f data.txt ) then echo "File exists"; fi
Cif [[ -f data.txt ]]; then echo "File exists"; end
Dif [ -f data.txt ]; then echo "File exists"; fi
Attempts:
2 left
💡 Hint
Remember the correct syntax for the test command and if statement.
🔧 Debug
advanced
2:00remaining
Find the error in this automation script snippet
What error does this Bash script produce? code: count=0 while [ $count -lt 3 ] do echo "Count is $count" count=$count+1 done
ASyntaxError: missing 'then' keyword
BOutputs 'Count is 0' infinitely
Ccount variable does not increment, infinite loop
DScript runs correctly and stops after 3 counts
Attempts:
2 left
💡 Hint
Check how the count variable is updated inside the loop.
🚀 Application
expert
2:00remaining
Automate file backup with timestamp pattern
Which Bash command correctly copies 'report.txt' to a backup file with the current date appended (format YYYY-MM-DD)?
Acp report.txt report_$(date +%Y-%m-%d).txt
Bcp report.txt report_$(date %Y-%m-%d).txt
Ccp report.txt report_$(date +%y-%m-%d).txt
Dcp report.txt report_$(date +%Y/%m/%d).txt
Attempts:
2 left
💡 Hint
Check the correct syntax for date formatting in command substitution.

Practice

(1/5)
1. Why are patterns useful in bash scripting automation?
easy
A. They provide reusable solutions to common tasks.
B. They make scripts run faster by using special commands.
C. They allow scripts to run without any errors always.
D. They automatically fix bugs in your code.

Solution

  1. Step 1: Understand what patterns mean in scripting

    Patterns are common ways to solve repeated problems efficiently.
  2. Step 2: Identify the benefit of using patterns

    They save time and reduce errors by reusing tested solutions.
  3. Final Answer:

    They provide reusable solutions to common tasks. -> Option A
  4. Quick Check:

    Patterns = reusable solutions [OK]
Hint: Patterns = repeatable solutions that save time [OK]
Common Mistakes:
  • Thinking patterns speed up script execution
  • Believing patterns fix all bugs automatically
  • Confusing patterns with error-free guarantees
2. Which of the following is the correct syntax to start a bash script with a pattern for error handling?
easy
A. #!/bin/bash error -set
B. #!/bin/bash set -e
C. #!/bin/bash set error
D. #!/bin/bash set +e

Solution

  1. Step 1: Recall the standard bash shebang line

    It starts with #!/bin/bash to specify the interpreter.
  2. Step 2: Identify the correct command to stop on errors

    set -e tells bash to exit on any error, a common pattern.
  3. Final Answer:

    #!/bin/bash set -e -> Option B
  4. Quick Check:

    set -e = exit on error pattern [OK]
Hint: Use 'set -e' to stop script on errors [OK]
Common Mistakes:
  • Using 'set +e' which disables error exit
  • Writing 'error -set' which is invalid syntax
  • Confusing 'set error' as a command
3. What will be the output of this script snippet using a common pattern to loop over files?
for file in *.txt; do
  echo "$file"
done
medium
A. Prints the string '*.txt' literally
B. Throws an error if no .txt files exist
C. Lists all files ending with .txt in the current directory
D. Lists all files in the directory regardless of extension

Solution

  1. Step 1: Understand the for loop pattern over files

    The loop iterates over all files matching the pattern '*.txt'.
  2. Step 2: Determine what echo prints each iteration

    It prints the current filename matching the pattern.
  3. Final Answer:

    Lists all files ending with .txt in the current directory -> Option C
  4. Quick Check:

    for file in *.txt = list .txt files [OK]
Hint: for file in *.txt loops over matching files [OK]
Common Mistakes:
  • Expecting literal '*.txt' output
  • Assuming error if no matches (depends on shell settings)
  • Thinking it lists all files, not filtered
4. This script is intended to stop on errors but does not. What is the error?
#!/bin/bash
set +e
cp file1.txt file2.txt
medium
A. Using 'set +e' disables exit on error, so script continues
B. Missing quotes around filenames causes error
C. The shebang line is incorrect
D. The cp command syntax is wrong

Solution

  1. Step 1: Check the meaning of 'set +e'

    It disables the exit-on-error behavior, allowing script to continue.
  2. Step 2: Understand why script does not stop on error

    Because 'set +e' is used, errors won't stop the script.
  3. Final Answer:

    Using 'set +e' disables exit on error, so script continues -> Option A
  4. Quick Check:

    set +e disables error exit [OK]
Hint: Use 'set -e' to stop on errors, not 'set +e' [OK]
Common Mistakes:
  • Confusing +e and -e options
  • Thinking quotes are mandatory for cp here
  • Assuming shebang line causes error
5. You want to create a bash script pattern that processes only non-empty text files in a directory. Which snippet correctly applies this pattern?
hard
A. for f in *.txt; do echo "$f"; done
B. for f in *.txt; do if [[ -z "$f" ]]; then echo "$f"; fi; done
C. for f in *.txt; do if [[ -f "$f" ]]; then echo "$f"; fi; done
D. for f in *.txt; do if [[ -s "$f" ]]; then echo "$f"; fi; done

Solution

  1. Step 1: Identify the test for non-empty files

    Using [[ -s "$f" ]] checks if file size is greater than zero (non-empty).
  2. Step 2: Confirm the loop processes only .txt files and echoes them if non-empty

    The loop iterates over *.txt files and echoes only those passing the size test.
  3. Final Answer:

    for f in *.txt; do if [[ -s "$f" ]]; then echo "$f"; fi; done -> Option D
  4. Quick Check:

    Use [[ -s file ]] to check non-empty files [OK]
Hint: Use [[ -s file ]] to filter non-empty files [OK]
Common Mistakes:
  • Using [[ -z ]] which checks for empty files
  • Using only [[ -f ]] which checks if file exists but not size
  • Not filtering files at all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard