Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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 instead of files.
Using '-r' or '-w' which check permissions, not existence.
✗ Incorrect
The '-f' operator checks if the file exists and is a regular file.
2fill in blank
mediumComplete the code to check if the directory '/home/user/docs' exists.
Bash Scripting
if [ [1] /home/user/docs ]; then echo "Directory exists." fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-f' which checks for regular files, not directories.
Using '-e' which checks existence but not specifically for directories.
✗ Incorrect
The '-d' operator checks if the path exists and is a directory.
3fill in blank
hardFix the error in the code to check if the file 'script.sh' is executable.
Bash Scripting
if [ [1] script.sh ]; then echo "File is executable." fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-r' or '-w' which check read or write permissions instead.
Using '-f' which checks if it is a regular file but not executable.
✗ Incorrect
The '-x' operator checks if the file is executable.
4fill in blank
hardFill both blanks to check if 'config.cfg' exists and is readable.
Bash Scripting
if [ [1] config.cfg ] && [ [2] config.cfg ]; then echo "Config exists and is readable." fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-f' instead of '-e' for existence check.
Using '-w' instead of '-r' for read permission.
✗ Incorrect
Use '-e' to check existence and '-r' to check read permission.
5fill in blank
hardFill all three blanks to create a dictionary of files in 'docs' directory that exist, are writable, and executable.
Bash Scripting
files_status = {file: ( [ -[1] file ] && [ -[2] file ] && [ -[3] file ]) for file in $(ls docs) } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up read and write permissions.
Using '-f' instead of '-e' for existence.
✗ Incorrect
Use '-e' for existence, '-w' for write permission, and '-x' for execute permission.