Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The asterisk (*) matches all files in the current directory, so the loop prints each file name.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-d' which checks for directories only.
Using '-s' which checks if file size is greater than zero.
✗ Incorrect
The '-e' option checks if the file exists, regardless of type.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ls -l' which lists files, not lines.
Using 'grep -l' which lists filenames matching a pattern.
✗ Incorrect
'wc -l' counts the number of lines in the input. The original code misses the command 'wc'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'num' without '$' prints the word 'num' literally.
Using 'num' instead of a sequence in the loop.
✗ Incorrect
'{1..5}' generates numbers 1 to 5. '$num' prints the current number in the loop.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%n' which returns the file name, not size.
Using incorrect keys in the associative array.
✗ Incorrect
'*' matches all files. 'file' is the key for the associative array. '%s' gets file size in bytes with stat.