Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Why scripts often process text in Bash Scripting - Test Your Understanding

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

Complete the code to print the first word of the line.

Bash Scripting
echo "Hello world" | cut -d ' ' -f [1]
Drag options to blanks, or click blank then click option'
A1
B2
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as field number causes no output.
Using 2 or 3 extracts the second or third word, not the first.
2fill in blank
medium

Complete the code to count lines containing the word 'error'.

Bash Scripting
grep 'error' logfile.txt | wc -l > [1]
Drag options to blanks, or click blank then click option'
Aoutput.txt
Bresult.txt
Ccount.txt
Derrors.log
Attempts:
3 left
💡 Hint
Common Mistakes
Using a log file name may confuse output with input.
Not redirecting output causes no file to be created.
3fill in blank
hard

Fix the error in the code to replace 'foo' with 'bar' in file.txt.

Bash Scripting
sed -i 's/[1]/bar/g' file.txt
Drag options to blanks, or click blank then click option'
Abar
Bfoo
Cfile
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'foo' and 'bar' reverses the replacement.
Using unrelated words causes no change.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

Bash Scripting
declare -A lengths
for word in apple banana cat dog; do
  if [[ ${#word} [1] 3 ]]; then
    lengths[[2]]=${#word}
  fi
done
Drag options to blanks, or click blank then click option'
A>
Bword
C<
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > reverses the condition.
Using len instead of word as key causes errors.
5fill in blank
hard

Fill all three blanks to filter lines containing 'error' and count unique occurrences.

Bash Scripting
grep [1] logfile.txt | sort | uniq -c | sort [2] -k1,1 | head -n [3]
Drag options to blanks, or click blank then click option'
A'error'
B-nr
C5
D-r
Attempts:
3 left
💡 Hint
Common Mistakes
Using -r instead of -nr sorts incorrectly.
Not limiting head to 5 lines shows too many results.

Practice

(1/5)
1. Why do many bash scripts process text files instead of other file types?
easy
A. Because text files are easy to read and manipulate with simple commands
B. Because binary files are faster to process
C. Because text files take up less disk space
D. Because scripts cannot open images or videos

Solution

  1. Step 1: Understand file types in scripting

    Text files contain readable characters, making them easy to handle with commands like grep and sed.
  2. Step 2: Compare text and binary files

    Binary files need special tools and are harder to manipulate directly in scripts.
  3. Final Answer:

    Because text files are easy to read and manipulate with simple commands -> Option A
  4. Quick Check:

    Text files = easy manipulation [OK]
Hint: Text files are simple to handle with basic commands [OK]
Common Mistakes:
  • Thinking binary files are easier to process
  • Assuming scripts can't open non-text files
  • Confusing file size with ease of processing
2. Which of the following is the correct syntax to search for the word error in a file named log.txt using grep?
easy
A. grep 'log.txt' error
B. grep error log.txt
C. grep -f error log.txt
D. grep log.txt error

Solution

  1. Step 1: Identify grep command syntax

    The correct syntax is grep [pattern] [file].
  2. Step 2: Match pattern and file

    Here, error is the pattern and log.txt is the file to search.
  3. Final Answer:

    grep error log.txt -> Option B
  4. Quick Check:

    grep pattern file = correct syntax [OK]
Hint: Remember grep syntax: grep pattern filename [OK]
Common Mistakes:
  • Swapping pattern and filename
  • Using incorrect flags like -f
  • Putting filename before pattern
3. What will be the output of this bash command?
echo "apple banana cherry" | awk '{print $2}'
medium
A. apple
B. cherry
C. apple banana
D. banana

Solution

  1. Step 1: Understand the input and awk command

    The input string is "apple banana cherry". Awk splits input by spaces and $2 refers to the second word.
  2. Step 2: Identify the second word

    The second word is "banana".
  3. Final Answer:

    banana -> Option D
  4. Quick Check:

    awk $2 = second word = banana [OK]
Hint: awk $2 prints the second word from input [OK]
Common Mistakes:
  • Confusing $1 and $2 fields
  • Expecting full line output
  • Not splitting input by spaces
4. This script is intended to replace all occurrences of 'cat' with 'dog' in a file named pets.txt. What is wrong?
sed 's/cat/dog/g pets.txt'
medium
A. sed cannot replace text
B. File name should come before the command
C. Missing closing single quote after g
D. The command should use double quotes instead of single quotes

Solution

  1. Step 1: Check sed command syntax

    The substitution command must be enclosed in quotes fully: 's/cat/dog/g'
  2. Step 2: Identify the error in the given command

    The closing single quote is missing after the g, causing a syntax error.
  3. Final Answer:

    Missing closing single quote after g -> Option C
  4. Quick Check:

    Quotes must enclose entire sed expression [OK]
Hint: Check for matching quotes around sed expressions [OK]
Common Mistakes:
  • Forgetting to close quotes
  • Misplacing file name inside quotes
  • Thinking sed can't replace text
5. You want to extract all lines containing the word error from multiple log files and save them into a single file errors.txt. Which script snippet correctly does this?
hard
A. grep error *.log > errors.txt
B. grep error errors.txt > *.log
C. grep > errors.txt *.log
D. grep *.log error > errors.txt

Solution

  1. Step 1: Understand grep usage with multiple files

    Using grep error *.log searches for 'error' in all files ending with .log.
  2. Step 2: Redirect output to a file

    The > operator sends all matching lines into errors.txt.
  3. Final Answer:

    grep error *.log > errors.txt -> Option A
  4. Quick Check:

    grep pattern files > outputfile [OK]
Hint: Use grep pattern files > outputfile to save matches [OK]
Common Mistakes:
  • Placing > before files
  • Swapping pattern and files
  • Using incorrect order of arguments
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