0
0
Bash Scriptingscripting~10 mins

File test operators (-f, -d, -e, -r, -w, -x) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

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-d
B-f
C-r
D-w
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-d' which checks for directories instead of files.
Using '-r' or '-w' which check permissions, not existence.
2fill in blank
medium

Complete 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'
A-d
B-f
C-e
D-x
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.
3fill in blank
hard

Fix 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'
A-f
B-r
C-w
D-x
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.
4fill in blank
hard

Fill 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'
A-e
B-r
C-f
D-w
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-f' instead of '-e' for existence check.
Using '-w' instead of '-r' for read permission.
5fill in blank
hard

Fill 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'
Ae
Bw
Cx
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up read and write permissions.
Using '-f' instead of '-e' for existence.