Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the file '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 only for directories.
Using -f which checks only for regular files.
Using -s which checks if file exists and is not empty.
✗ Incorrect
The -e option checks if a file or directory exists.
2fill in blank
mediumComplete the code to check if 'log.txt' is a regular file.
Bash Scripting
if [ [1] "log.txt" ]; then echo "It's a regular file" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks for directories.
Using -e which checks for any file or directory.
Using -r which checks if file is readable.
✗ Incorrect
The -f option checks if the file exists and is a regular file (not a directory).
3fill in blank
hardFix the error in the code to check if 'folder' is a directory.
Bash Scripting
if [ [1] "folder" ]; then echo "It's a directory" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f which checks for regular files.
Using -e which checks for any file or directory.
Using -s which checks if file is not empty.
✗ Incorrect
The -d option checks if the path is a directory.
4fill in blank
hardFill both blanks to check if 'script.sh' exists and is executable.
Bash Scripting
if [ [1] "script.sh" ] && [ [2] "script.sh" ]; then echo "Script is ready to run" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e instead of -f to check for regular file.
Using -r instead of -x to check for executable permission.
✗ Incorrect
-f checks if it is a regular file, and -x checks if it is executable.
5fill in blank
hardFill all three blanks to create a dictionary of files and their sizes if size is greater than 0.
Bash Scripting
declare -A file_sizes for file in *; do if [ [1] "$file" ] && [ $( [2] "$file" ) -gt 0 ]; then file_sizes[[3]]=$(stat -c%s "$file") fi done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f instead of -s to check file size.
Using filename incorrectly as dictionary key.
Not using stat command to get file size.
✗ Incorrect
-s checks if file size is greater than zero, stat -c%s gets file size, and $file is the key in the dictionary.