We use IF LOOP all time, but when it comes to multiple conditions IF Loop gets complicated as you have to write multiple times. CASE saves going through whole if…then…else statements.
#!/bin/bash
echo -n "Enter the Country name "
read Country
echo -n "Captial of $Country is "
case $Country in
India )
echo -n "Delhi"
echo ""
;;
Argentina )
echo -n "Buenos Aires"
echo ""
;;
Brazil )
echo -n "BrasÃlia"
echo ""
;;
* )
echo -n "unknown"
;;
esac
So, if you enter the country name which is mentioned it will give desired output and if it’s not present It will show unknown as mentioned in script.
[root@server ~]# ./case.sh
Enter the Country name India
Captial of India is Delhi
[root@server ~]# ./case.sh
Enter the Country name Argentina
Captial of Argentina is Buenos Aires
[root@server ~]# ./case.sh
Enter the Country name USA
Captial of USA is unknown
[root@server ~]#