hey guys welcome to the next blog on shell scripting tutorial for beginners in the last blog we have seen how to use for loop in shell scripting now in this blog we will see how we can use a for loop with some command and we will see some examples to understand this better so this is the basic syntax we have seen in the last blog so we use for keyboard and then command or any variable here and then we use the keyword in and then after the keyword we give some input in the form of some lists or some commands right and then we use do and done keyword and then we use some come on to you know execute the item value right which is here okay so let's see how we can use commands here so we can give our list of commands in order to execute them so for example I can give LS here and then I can give the PWD for present working directory and for example I give the date command to know the date right so this is the list of command and these commands will be executed one by one so first of all the LS command will be executed and then the PWD command will be executed and then the date command will be executed right so let's see how this works so what I'm going to do is I am going to echo for example online and then I'm going to print the name of the command so let's print the name of the command first and then we are going to execute the command itself so how we can execute the command itself we can execute it without the echo command so echo command is going to give you the name of the command and then this is going to actually execute that command okay so let's run the script and let's see what happens so I'm going to run my script and this is the output so the LS command is giving us all these LS command values right and then the PWD command is going to give us this output and the date command has given us this output right so we can execute a number of commands like this and they will be executed one by one using this method right now let's take some practical example also so for example I'm in my home folder right now and then I want to print all the directories which are there in my home folder for example right so let me do LS minus a here and I can see there are some directories here and I want to print all the directories which are there in this folder so I'm going to clear the terminal first of all and then let's write the code so here
I can use Astrix and Astrix means here that I want to iterate over each and every file or directory in this directory which I am currently in right so as strix is a wild-card and hopefully you know how it works and then what I'm going to do is instead of this code in the do and done keyword we will first of all use the if statement and we will evaluate the value of command and command is not appropriate keyword here for example I will say item here right so whatever item is iterated or whatever item we have using this Asterix keyword then we will evaluate if it's a directory or not and if it's a directory then we will print it so we will use - D for this and then the name of the item right and then we use the keyword then as we do with the if-else conditions and then fi - n viii right and in here we will just echo this directory name okay so just right echo and then the name of the item so what it's not going to do this Astrix command it's going to see all the items which are there in this directory it can be file or it can be a directory name and then we are checking using this code whether this is a directory or a file and if it's a directory then we will print the name of it okay so let's run the script and let's see what happens and we run the script and you can see it's giving us all the directory names so this is the name of the directory name of the directory name of the directory and then all the name of the directories right now in this output we can also see this warning or error also here and let's see why this error is coming so I'm going to open this directory which is printing this output and I can see there is a file name and this file name has you know the keywords then this file name has a qrt function is da so there are four keywords separated by spaces here right and now our script is confused that these names are separate or these names is the name of one file so for example I will delete this file name and let's run the script once again so let me run it and now it prints all the directories like this okay so let me clear the terminal and for example I just want to print all the files so instead of using - tea I can use - f or choosing all the files and I'm going to run the script once again and now it prints all the files which are there in this directories okay so in this way we can use commands with for loop I hope you enjoyed this blog 😊
Post a Comment