Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign a default value 'guest' to the variable 'user' if no input is given.
Bash Scripting
user=${1:-[1]
echo "User is $user" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 directly without default value syntax.
Using = instead of :- inside the braces.
✗ Incorrect
The syntax ${1:-guest} assigns 'guest' if $1 is empty or unset.
2fill in blank
mediumComplete the code to assign '8080' as the default port if no argument is provided.
Bash Scripting
port=${1:-[1]
echo "Server will run on port $port" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use the :- syntax for default values.
Using a port number that is not the intended default.
✗ Incorrect
The default port 8080 is assigned if no input is given using ${1:-8080}.
3fill in blank
hardFix the error in the code to correctly assign 'default.txt' as the filename if no argument is given.
Bash Scripting
filename=${1[1]"default.txt"}
echo "Using file: $filename" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':-' causes wrong behavior when variable is empty.
Omitting the colon ':' changes the meaning.
✗ Incorrect
The correct syntax for default value is ${var:-default}, so ':-' is needed.
4fill in blank
hardFill both blanks to assign 'admin' as default user and 'root' as default group if no inputs are given.
Bash Scripting
user=${1[1]"admin"}
group=${2[2]"root"}
echo "User: $user, Group: $group" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Mixing different default value syntaxes for user and group.
✗ Incorrect
Use ':-' to assign default if variable is unset or empty for both user and group.
5fill in blank
hardFill all three blanks to create a script that assigns default values: 'localhost' for host, '3306' for port, and 'root' for user if no inputs are given.
Bash Scripting
host=${1[1]"localhost"}
port=${2[2]"3306"}
user=${3[3]"root"}
echo "Connecting to $host on port $port as $user" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Using '-' without colon which only covers unset variables.
✗ Incorrect
The ':-' syntax is used for all three variables to assign defaults if unset or empty.