We have used if loop and while loop multiple times and without it no bash script has been completed. But at a times using those loops are very much helpful and you have to use CASE. Let’s see more about case in shell scripting
Below is the sample code, which will print the capital after you enter the country name. Just try to write the same code in If loop, It would be very long, untidy and hard to manage.
#!/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
Let’s try to run the above code. It will ask you to first entry the country name it will print it’s capital and if the entry is not mentioned it will just print unknown.
[[email protected] ~]# ./case.sh Enter the Country name India Captial of India is Delhi [[email protected] ~]# ./case.sh Enter the Country name Argentina Captial of Argentina is Buenos Aires [[email protected] ~]# ./case.sh Enter the Country name USA Captial of USA is unknown [[email protected] ~]#
We have bunch of information available on scripting.