0
0
Bash Scriptingscripting~10 mins

Portable scripting (POSIX compliance) 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 print the current working directory in a POSIX-compliant way.

Bash Scripting
echo [1]
Drag options to blanks, or click blank then click option'
Agetcwd
Bcwd
Cpwd
Ddir
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-POSIX commands like 'cwd' or 'getcwd' which may not exist.
Using 'dir' which is not guaranteed to be POSIX-compliant.
2fill in blank
medium

Complete the code to check if a file named 'data.txt' exists in a POSIX-compliant way.

Bash Scripting
if [ [1] "data.txt" ]; then echo "File exists"; fi
Drag options to blanks, or click blank then click option'
A-d
B-e
C-f
D-s
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 size is greater than zero.
3fill in blank
hard

Fix the error in the script to assign the output of the 'date' command to a variable in a POSIX-compliant way.

Bash Scripting
current_date=[1]
Drag options to blanks, or click blank then click option'
A`date`
B$(date +%Y-%m-%d)
C${date}
D$(date)
Attempts:
3 left
💡 Hint
Common Mistakes
Using ${date} which tries to expand a variable named date.
Using backticks which can cause issues in nested commands.
Using $(date +%Y-%m-%d) when the question wants just the output of date command.
4fill in blank
hard

Fill both blanks to create a POSIX-compliant loop that prints numbers 1 to 5.

Bash Scripting
i=1
while [ [1] ]; do
  echo $i
  i=[2]
 done
Drag options to blanks, or click blank then click option'
A$i -le 5
B$i -lt 5
C$((i + 1))
D$i + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '$i -lt 5' which stops at 4, not 5.
Using 'i=$i + 1' which is not arithmetic expansion.
Using 'i=$i++' which is not POSIX-compliant.
5fill in blank
hard

Fill all three blanks to create a POSIX-compliant script that creates a directory if it does not exist and then changes into it.

Bash Scripting
if [ ! -d [1] ]; then
  [2] [3]
fi
Drag options to blanks, or click blank then click option'
Amyfolder
Bmkdir
C-p
Dcd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cd' in the blank where directory creation is expected.
Omitting the '-p' option which is safer for nested directories.
Using a different directory name than 'myfolder'.