0
0
Bash Scriptingscripting~10 mins

Default values for input 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 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'
A$1
Bdefault
Cguest
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 directly without default value syntax.
Using = instead of :- inside the braces.
2fill in blank
medium

Complete 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'
A8080
B3000
C443
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use the :- syntax for default values.
Using a port number that is not the intended default.
3fill in blank
hard

Fix 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'
A:-
B:=
C=
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':-' causes wrong behavior when variable is empty.
Omitting the colon ':' changes the meaning.
4fill in blank
hard

Fill 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'
A:-
B:=
C-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Mixing different default value syntaxes for user and group.
5fill in blank
hard

Fill 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'
A:-
B:=
C-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or ':=' which do not cover empty values.
Using '-' without colon which only covers unset variables.