Switch (Case) Statment in Bash The case statement is useful and processes faster than an else-if ladder. Instead of checking all if-else conditions, the case statement directly select the block to execute based on an input. Create the first program using the case statement in a shell script. The program takes input from the user and executes statements based on the input value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/bash read -p "Enter your choice [yes/no]:" choice case $choice in yes) echo "Thank you" echo "Your type: Yes" ;; no) echo "Ooops" echo "You type: No" ;; *) echo "Sorry, invalid input" ;; esac |
Save the above script in case1.sh and execute the script on bash shell. chmod +x case1.sh ./case1.sh Enter your choice [yes/no]:yes Thank you Your type: Yes…
Read More