0
0
Bash Scriptingscripting~10 mins

Why patterns solve common automation needs 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 all files in the current directory.

Bash Scripting
for file in [1]; do echo "$file"; done
Drag options to blanks, or click blank then click option'
A*
Becho
Cpwd
Dls
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ls' directly in the loop variable, which causes errors.
Using 'pwd' which prints the current directory path, not files.
2fill in blank
medium

Complete the code to check if a file named 'data.txt' exists.

Bash Scripting
if [[ [1] "data.txt" ]]; then echo "File exists"; fi
Drag options to blanks, or click blank then click option'
A-f
B-e
C-s
D-d
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-d' which checks for directories only.
Using '-s' which checks if file size is greater than zero.
3fill in blank
hard

Fix the error in the code to count lines in 'log.txt'.

Bash Scripting
count=$(cat log.txt | [1] -l)
Drag options to blanks, or click blank then click option'
Als
Bgrep
Cwc
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ls -l' which lists files, not lines.
Using 'grep -l' which lists filenames matching a pattern.
4fill in blank
hard

Fill both blanks to create a loop that prints numbers 1 to 5.

Bash Scripting
for num in [1]; do echo [2]; done
Drag options to blanks, or click blank then click option'
A{1..5}
Bnum
C$num
D1 2 3 4 5
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'num' without '$' prints the word 'num' literally.
Using 'num' instead of a sequence in the loop.
5fill in blank
hard

Fill all three blanks to create a dictionary-like structure with file sizes.

Bash Scripting
declare -A sizes
for file in [1]; do sizes[[2]]=$(stat -c [3] "$file"); done
for f in "${!sizes[@]}"; do echo "$f: ${sizes[$f]} bytes"; done
Drag options to blanks, or click blank then click option'
A*
Bfile
C%s
D%n
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%n' which returns the file name, not size.
Using incorrect keys in the associative array.