hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog I'm going to show you how we can read files using while loop now there are several ways of reading files using while loop and we are going to see some of them the first way of reading files using while loop is by using input redirection so we use this while loop skeleton and after this keyword while we will use the read command so just use read and then the variable in which we want to save the content of the file line-by-line okay and then what we are going to do is we will echo this variable so just write echo and dollar P which is this variable itself and then if you know file redirection we use angle bracket here so we are going to use this angle bracket and the name of the file whatever file you want to you know read for example I want to read this file itself which is my script file hello dot Sh right so
I can just write hello dot and such and then this is going to read this file and then this is going to go here in the parameter P and then using this echo is going to read it right and what is input 3 direction so input redirection is something like this that what wherever the pointing edge of this angle bracket is the stream of the read content goes to that direction so the file content inside this hello dot FH is redirected to this while loop right and then it is ready using the read command and then it's printed using the echo command right so let's run the script and let's see what happens and you can see it prints the content of the file which is our script itself now the second way off for reading the file is to read the file in a single variable then print it now if you know pipes then it's easier for you so I'm going to just remove this things from here and then what I'm going to do here is I'm going to first of all use a cat command so before this y keyword I'm going to use cat and then whatever file you want to read once again hello dot Sh and then I will use this pipe symbol and that's it so what's happening here is whatever is the content which is read using this cat command is like an input for this while command right so the content of this cat output is like an input to this while loop and then it's going to read it using this read command and then it's going to print the content of the file so once again let's run the script and once again you can see the same content is printed so the first way is by using the input redirection and second way is to you know read the file content into one variable and then print it now sometimes so it's hard to read the files using these two methods and the problem is some special characters in the file for example line indentation and other things so what we can do is in the last method is we will use this read commands once again but we will use the ifs now iff stands for internal field separator and it is used by the shell to determine how to do word splitting and that is how to recognize word boundaries right so let's see how we can do this using ifs so we will use the wild keyboard here and then in capital use ifs and then is equal to and then give a space here so it's important to note here is that we are not assigning the read to ifs we are assigning space to an ISS right so just give the space between this equal to sign and read the command and with this read command we have to use - our flag here and - our prevents the backslash escape from being interpreted right so this flag is going to prevent backslashes escapes from being interpreted right and instead of p you can also use line because it's more descriptive of this method and just use the line here okay now we are going to use once again input redirection here so use this angle bracket and then use the filename for example hello dot Sh okay and let's run the script once again and let's see what happens and you can see it's printing the content of the file once again now if you are confused here instead of using space here you can just use in single quotes and a space here like this also and once again once again you can run the script and the you know output will be the same you can see here right so you can do this also right and for example we want to read the file which is there in my etc' folder for example so let's say what are the content in my etc' folder so I'm going to just LS for example et Cie and you can see there are several files here and let's see if what we want to read from here so for example I want to read the host cons which is here this is the host comp so let me just cat the content of this and then we will read this file using our while loop so cat and then et Cie and then the filename host comp and this is the content of this file right so we will just copy the path of this file and paste it here okay and then once again we will run the script and then press it and you can see the same content is printed whatever is printed using the cat command right so you can use these three methods to read the file using while loop now there are some other methods of reading files for example using file descriptors and that we will see in the later blogs when we will learn what our file descriptors and other things right but these are the basic ways of reading the file using while loop so I hope you've enjoyed this BLOG 😊
إرسال تعليق