0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.