Shell Scripting Tutorial for Beginners 13 - The case statement Example


hey guys welcome to the next blog on shell scripting tools for beginners in the last blog we have seen how we can use the case statement in shell scripting now in this blog we will take one more example on case statements and we will consolidate our knowledge which we have gained in the last blog so we already know that how case statement works in scripting and this is the basic syntax which we have learned in the last blog so let's take an example and this time we are going to use some pattern to evaluate some cases so let's say we want to allow the user to enter any character and then we will evaluate that character and based upon the character entered by the user we will display if it's a capital letter or a small letter or a integer or some other special character or it's not a character he entered some string for example so let's get started so what I am going to do is on the top I am going to ask the user to enter some character so I will just use - II for this and I will say enter some character and then we will allow the user to enter the character on the same line right and then we will take that character using the read command so just write read and then we will save it for example in a value so just try it value variable and then we will take this variable so I am going to remove this variable because we don't need this variable so now our character or whatever input is entered by the user is in the value variable so we will evaluate this value expression and then we will check if it's a capital letter or small letter right for that we can use some patterns now how pattern works so for example we want to evaluate if user has entered the small letter A to Z so we can may use this square bracket and then you just need to write a and then minus Z and this pattern represent that we are expecting a small letter between A to Z okay in a similar way if you want to evaluate a capital character then we can just say capital A to Z like this so this is the pattern for capital A to Z right now if you want to evaluate the number any integer 0 to 9 so we can just write 0 to 9 right now if the user enters any other special character for example Asterix or some other characters which is not the part of e to Z or any number then we can evaluate it with the question mark now what is question mark question mark is a pattern which expects any special character any character which is one letter character right and this asterisk or star symbol R waits for more than one character so it can be one on more than one special character so if the user enters for example any string and not a character then it goes into this situation or in this case and this is for only one character any special character right and here we can echo that character so for example user entered value and then we can just say A to Z smaller letters write in a similar way for capital A to Z can just right here capital a to Zed and in the case of integer let's copy this and paste it here and we will just say 0 to 9 and in the case of question mark we will just say it's a special character right and in the last case we will just say unknown input so we will just write here unknown input so let's run the script and let's see what happens when we run this script let's run the script and let's enter any small letter for example app and then press Enter and say the user entered letter F which is between small a to small Z right once again let's run the script and this time we will enter the capital K for example and then press Enter and you can you see there is an error here because we were expecting this statement to be printed which is capital a to capital Z but here it prints user entered capital K but then it prints small a to Z so it goes into this condition and if you also get the same error then most probably you need to do a small hack and this is you just need to set the Lang as C so just write Lang is equal to C so in capital Lang is equal to capital C and this means you are setting a languor environment variable is equal to C right and this means that you are using the safe locale everywhere in your operating system so if it doesn't work for you for capital letter the script you just need to set this environment variable as C so lang is equal to C and once again we will run the script and once again we will enter the capital K and this time it shows the right result to us right so if you face the same problem just use this environment variable is equal to C right once again let's run the script and this time we will enter any number and press ENTER and says of course it's between zero to nine right or any other number for example five it's also between zero to nine right now let's enter any special character so I'm entering for example this and symbol which is a special character and then press Enter and it says user entered this and it's a special character now let's enter any random string and let's see what's the result so it says unknown input right so in this way you can match characters using these kind of patterns right for more information on patterns and regular expression you can always search for a regular expression and you will see a vicky pedia page on regular expression and you will be able to understand and read their what the square bracket is what is - what is this question mark stands for or what is this asterisk everything so the Wikipedia page for regular expression has a good explanation on pattern so just read that Wikipedia page and you will be able to understand these patterns in a better manner so case is not just used for our strict letters or characters you can you know evaluate any patterns using the case statement and that's the beauty of this case statement so I hope you enjoyed this blog 😊

Post a Comment

Previous Post Next Post

Recent in Technology News