0
0
Bash Scriptingscripting~10 mins

Why conditionals branch script logic 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 check if a file exists.

Bash Scripting
if [ -e [1] ]; then
  echo "File exists"
fi
Drag options to blanks, or click blank then click option'
Afilename
B-f
C/tmp
Dmyfile.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the test option like -f instead of the file name.
Leaving the blank empty.
2fill in blank
medium

Complete the code to print "Yes" if variable x equals 10.

Bash Scripting
x=10
if [ [1] -eq 10 ]; then
  echo "Yes"
fi
Drag options to blanks, or click blank then click option'
A$x
B10
C==
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using x without $ which compares string 'x' not the variable value.
Using == which is not for numeric comparison in test.
3fill in blank
hard

Fix the error in the conditional to check if a directory exists.

Bash Scripting
DIR="/home/user"
if [ [1] "$DIR" ]; then
  echo "Directory exists"
fi
Drag options to blanks, or click blank then click option'
A-x
B-e
C-d
D-f
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f which checks for regular files, not directories.
Using -e which checks for any file type but not specifically directory.
4fill in blank
hard

Fill both blanks to check if variable num is greater than 5 and print a message.

Bash Scripting
num=7
if [ [1] [2] 5 ]; then
  echo "Greater than five"
fi
Drag options to blanks, or click blank then click option'
A$num
B-gt
C-lt
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable name without $.
Using -lt which means less than.
5fill in blank
hard

Fill both blanks to create a conditional that checks if a string is empty.

Bash Scripting
str=""
if [ [1] [2] ]; then
  echo "String is empty"
fi
Drag options to blanks, or click blank then click option'
A-z
B$str
C""
D-n
Attempts:
3 left
💡 Hint
Common Mistakes
Using -n which checks for non-empty string.
Using str without the leading $.