hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog we will see how to use select loop in shell scripting now first of all why he use select loop so the select construct allows us to generate easy menus so whenever you need to write a script which requires some menus then you can use select loop now let's see the basic syntax of select loop so to use select you just need to write this select keyword and then you need to give any variable name and then this keyword in and then you can give a list of items here and then in do and done in between do and done keywords you can execute some command based upon the list you have provided so let's see actually how we use select loop so for example I have the list of some names so I'm going to declare a name variable here and the list can be some names so for example mark John Tom Ben and so on right so right now I'm just using four names and then I can just print those names like this so I can just write echo and then selected name is whatever is selected so dollar and the name which is this variable right and then we can write selected okay and I'm going to just save my script and I'm going to run this script and let's see what happens so I'm going to just press ENTER and you see here it gives us these list of names as a menu so every name is given and before every name we have a number provided to us right so whatever list you will give here
it will be presented like this with a number before it in a sequence and then the prompt will ask you to give any number from this list so for example one is associated to mark two is associated to John three is associated to Tom and four is associated to Ben so when I write two and then press Enter it prints John selected right because we selected the second item which is associated to John right and this is the easy usage of the Select loop so select loop construct is similar to follow but what it does is first of all it iterate over the list and give you this kind of menu structure to us and then we can select any of the items from the menu and then execute some commands or statement based upon the selected value right now select loop is often used with the case statement so some kind of cases can be used with the Select loop and we will see how we can use it so what I'm going to do is first of all I'm going to give the basic structure of cases so case and e-s-a see right this is the basic construct and then we can add some you know cases here right so what we want to do here is we will just see what is the name provided so let's just use this dollar symbol and name variable and then in we already know what is the basic syntax of this case statement right and then we can just give names from this list so whenever mark is selected then we can print something right so just give this and then in this case what we can do is we can echo for example mark selected write something like this and then to end you can just give double semicolon like this okay so similarly we will just give multiple cases here for the four names and you can even provide the default case here so if you want to provide default case you can just write this kind of syntax as the default case right so in this case we will just echo and then we can just print error and we can also say please provide a number between one to four so we can just say please provide the number between one to four right so you can just write this in the case of any of the default cases which is not present in this case list and let's run the script once again so you can see whenever you you know select or give any number it's not going to you know exit out of this menu you can give multiple two times the number and it's going to once again ask you the number so in order to exit the script you just need to press ctrl C in order to exit out of this script right and then once again run the script and you have these numbers once again and then once again when I give one it says mark selected to John selected three Tom selected an for been selected right so this select loop is often used with this case statement I am just giving you this kind of echo statement here but you can execute any complex logic based upon the selected value from the Select loop ok so this is the basic usage of select loop in shell scripting I hope you've enjoyed this blog 😊
إرسال تعليق