Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Report generation script in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the current date in the report header.

Bash Scripting
echo "Report generated on: $(date [1])"
Drag options to blanks, or click blank then click option'
A-d
B+%H:%M:%S
C+%Y-%m-%d
D-u
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-d' which expects a date string, not a format.
Using '%H:%M:%S' without '+' which is invalid.
Using '-u' which changes timezone, not format.
2fill in blank
medium

Complete the code to count the number of lines in the file 'data.txt'.

Bash Scripting
line_count=$(wc -l [1])
Drag options to blanks, or click blank then click option'
A-w data.txt
Bdata.txt
C-c data.txt
D-m data.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Adding extra flags like '-w' or '-c' which count words or bytes.
Putting the filename before the option '-l'.
3fill in blank
hard

Fix the error in the script to append the summary to 'report.txt'.

Bash Scripting
echo "Summary: Total lines = $line_count" [1] report.txt
Drag options to blanks, or click blank then click option'
A>>
B|
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which overwrites the file.
Using '<' which is input redirection.
Using '|' which pipes output to another command.
4fill in blank
hard

Fill both blanks to create a loop that processes each line in 'data.txt'.

Bash Scripting
while IFS=[1] read -r [2]; do
  echo "Processing: $[2]"
done < data.txt
Drag options to blanks, or click blank then click option'
A""
Bline
Cword
D' '
Attempts:
3 left
💡 Hint
Common Mistakes
Setting IFS to a space which splits words, not lines.
Using 'word' which is misleading for line reading.
5fill in blank
hard

Fill all three blanks to create a dictionary-like summary of word counts from 'data.txt'.

Bash Scripting
declare -A word_counts
while read -r [1]; do
  ((word_counts[[2]][3]1))
done < data.txt
Drag options to blanks, or click blank then click option'
Aword
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' which subtracts instead of adds.
Using different variable names causing errors.

Practice

(1/5)
1. What is the main purpose of a report generation script in bash?
easy
A. To automatically collect and save information into a file
B. To compile source code into an executable
C. To delete all files in a directory
D. To open a graphical user interface

Solution

  1. Step 1: Understand the script's goal

    A report generation script is designed to gather data and save it for review.
  2. Step 2: Match purpose with options

    Only To automatically collect and save information into a file describes collecting and saving information automatically.
  3. Final Answer:

    To automatically collect and save information into a file -> Option A
  4. Quick Check:

    Report generation = collect and save info [OK]
Hint: Think: What does 'report generation' mean? Collect and save! [OK]
Common Mistakes:
  • Confusing report generation with compiling code
  • Thinking it deletes files
  • Assuming it opens GUIs
2. Which line correctly starts a bash script for report generation?
easy
A. echo "Start script"
B. #!/bin/python
C. #!/bin/bash
D. import bash

Solution

  1. Step 1: Identify the correct shebang for bash

    The first line in a bash script must be #!/bin/bash to specify the interpreter.
  2. Step 2: Check other options

    Options B, C, and D are either for other languages or commands, not the script start line.
  3. Final Answer:

    #!/bin/bash -> Option C
  4. Quick Check:

    Shebang for bash = #!/bin/bash [OK]
Hint: Always start bash scripts with #!/bin/bash [OK]
Common Mistakes:
  • Using wrong interpreter path
  • Starting with commands instead of shebang
  • Confusing bash with python
3. What will be the output of this script?
#!/bin/bash

filename="report.txt"
echo "Report generated on: $(date)" > $filename
echo "User count: $(who | wc -l)" >> $filename
cat $filename
medium
A. Shows an error because of wrong variable usage
B. Prints current date and number of logged-in users in report.txt
C. Prints only the date, not user count
D. Creates an empty file named report.txt

Solution

  1. Step 1: Analyze echo commands and redirection

    The first echo writes the date line to report.txt (overwrite). The second echo appends user count.
  2. Step 2: Understand the final cat command

    cat prints the contents of report.txt, showing both lines.
  3. Final Answer:

    Prints current date and number of logged-in users in report.txt -> Option B
  4. Quick Check:

    echo > overwrite, echo >> append, cat prints file [OK]
Hint: Remember > overwrites, >> appends in bash [OK]
Common Mistakes:
  • Confusing > and >> operators
  • Thinking variable usage is wrong
  • Assuming file is empty
4. Identify the error in this report generation script snippet:
#!/bin/bash
filename=report.txt
echo "Start report" > $filename
echo "Date: $(date)" > $filename
cat $filename
medium
A. The second echo overwrites the file, losing the first line
B. The variable filename is not defined correctly
C. Missing execute permission on the script
D. The cat command is incorrect

Solution

  1. Step 1: Check file redirection operators

    The first echo writes "Start report" to the file. The second echo uses > again, overwriting the file.
  2. Step 2: Understand the effect on output

    Because of overwrite, the first line is lost. Only the date line remains in the file.
  3. Final Answer:

    The second echo overwrites the file, losing the first line -> Option A
  4. Quick Check:

    Using > twice overwrites file [OK]
Hint: Use >> to append, not > to avoid overwriting [OK]
Common Mistakes:
  • Assuming > appends instead of overwriting
  • Thinking variable is undefined
  • Blaming cat command
5. You want to create a report script that lists all files in the current directory and their sizes, saving to files_report.txt. Which script snippet correctly does this?
hard
A. #!/bin/bash ls -l > files_report.txt
B. #!/bin/bash ls -l >> files_report.txt ls -a > files_report.txt
C. #!/bin/bash ls -lh > files_report.txt
D. #!/bin/bash ls -lh > files_report.txt wc -l >> files_report.txt

Solution

  1. Step 1: Understand the requirements

    The report must list files with sizes and save to files_report.txt.
  2. Step 2: Analyze options for correct commands and output

    #!/bin/bash ls -lh > files_report.txt wc -l >> files_report.txt uses ls -lh for human-readable sizes and writes to the file. Then it appends the line count with wc -l, adding useful info.
  3. Step 3: Check other options

    #!/bin/bash ls -l > files_report.txt lacks human-readable sizes. #!/bin/bash ls -l >> files_report.txt ls -a > files_report.txt overwrites file after appending, losing data. #!/bin/bash ls -lh > files_report.txt misses line count info.
  4. Final Answer:

    Option D correctly lists files with sizes and appends line count to the report -> Option D
  5. Quick Check:

    ls -lh for sizes + wc -l appended = correct report [OK]
Hint: Use ls -lh for sizes and append extra info with >> [OK]
Common Mistakes:
  • Overwriting file after appending
  • Not using human-readable size option
  • Missing extra useful info like line count
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