hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will learn how to use case statement in scripting so the case statement is good alternate for multi-level if-then-else condition or statements it enables us to match several values against one value now what is the basic syntax for a case statement so let me show you the basic syntax for case statement so this is the basic syntax for case statement you use the keyword case and then you give any expression to evaluate right this can be a variable name or any expression to evaluate then you will give the keyword in and then you match multiple patterns so you can have multiple cases to evaluate so you can use a pattern here it can be a strict pattern or flexible pattern like regular expression and then you use this parent is closing parentheses to show that this is the end of this case and then comes the statement after this case so if the case is true then the statement is executed and then this double semicolon indicates the script that this case statement has been finished so start using the second case or start evaluating the second case right and with the second case also you can use a statement and then the third case comes and fourth case and then all the case comes and at the end you have this e sack or the reverse of case so from right hand side you can seek CAS e but it's the reverse of for this case spelling right so how you can use that case let's take an example and let's see how we can you know actually use the case statement in real life so let's say we want to evaluate a rental of some vehicle so to that for example I will just take an argument with the script so I will take an argument with the script when I run the script and this argument I will save in for example in a variable and variable name I will name it as a vehicle is equal to and then the fourth argument which is passed using the script I will just pass it to the variable vehicle ok and then I will evaluate this vehicle so this expression comes here and it can be variable also so you can just evaluate this vehicle here so case and then dollar symbol and then the name of the variable which contains the name of the vehicle or type of the weaker and then this in and then comes the pattern okay so for now we will just see the strict pattern here or compare the strict pattern so if this vehicle is for example car we will just execute some statement for example we will echo that the rent of this car is something so let's say we will print went off for the vehicles in our case which is car is $100 for example right in the similar manner you can echo second statement so if the vehicle is for example van then we will see a rent of the van is $80 right and in the same way we can add multiple pattern matching here so let me add one more pattern for example we will get the input bicycle so bicycle rent is for example $20 or let's say $5 okay and let's say user input truck so the rent of the truck is one $50 for example right and if the user inputs the any adult value other than these four values we want to catch this value which is a random value for example and this random value you can catch from star and then this parentheses okay now this asterisk means if you know Linux command line aspects means a wild-card which matches any text okay so in the default case this case will be followed and then we will say here we will echo here that this vehicle is unknown and we will just say the vehicle is unknown okay this is a default condition right so let's run the script and let's see what happens when we run the script so and then I will just run this script and it says unknown vehicle because I haven't given any argument with this script and this script expect that the first argument which is passed with the script is the vehicle type right so
let's run the script once again so let's say we enter car with this tent so the script name and the argument is car and you can see rent of the car is $100 right so this condition all expression is met so it echoes this statement right earlier we haven't given any argument here so it goes to the default case which is this case which is unknown vehicle right let's give some other case for example van and this time the van is $80 for example let's say we enter clock and this time the rent is one $50 for the truck right so in this way you can do the pattern matching and in this way you can use the case statement in scripting but here the pattern matching can be anything so right now we are matching the strict pattern so we are just matching whatever is entered using this argument but this can be anything so this case can be any regular expression or any pattern which you want to match so if you want to match some pattern you can do it also in this case and if you don't know what is pattern and how to use pattern just search for regular expression I think and you will have the good idea about pattern matching and you know regular expressions also ok so this is just one example of using case statement in the next blog I will take one more example of using case statement and we will consolidate our knowledge on case statements so stay tuned. i hope you enjoyed this BLOG
Post a Comment