Shell Scripting Tutorial for Beginners 7 - How to append output to the end of text file


hello guys welcome to the next blog on shell scripting tutorial for beginners in the last blog we have seen how to use file test operators in shell scripting now in this blog what we are going to do is you are going to you know consolidate our knowledge which you have gained in the last two blog's that is if conditions and the file test operators and we will create some useful examples from that so what in this blog I'm going to show you is how we can you know write a script to append some text to a file which already exists okay so let's get started what I'm going to do is I'm going to just edit the code I have written in the last blog so what I'm going to do is I'm going to just remove this code or let this code be there so first of all what I wanted the user to do is I want the user to ask him to give the file name as we have seen in the last blog so I will just echo enter the name of the file and then whatever name he enters I will save it into this variable and then what I'm going to do is I'm going to check whether this file exists or not it's a valid file or not so I will use the F flag for this and if this file exists then once again I will check whether I have the right permission to write some text into this file or not and then if I have the permission I will write into the file otherwise I will just show a message that this file doesn't have the right permission okay so in this if condition if this file exists what I will show is I will save file not exist and I'm going to remove this echo statement from here and in here I'm going to add one more if condition or if statement so let's use the if-statement so once again the if statement like this and then we will write the expression inside these the square brackets and here we will write them and then we want to evaluate the condition and if the condition is true we will allow the user to append to this file using cat and if the you know write permissions are not ok then we will write some echo message in the else condition right and then we will end this if statement by fi okay so here we want to check whether the file has right condition so I will just give a space and then minus W and then the file name variable which is this one and this condition will check whether the file have the right permission or not so this variable name here and if you haven't noticed in if condition inside this expression you know square brackets I always provide a space after this square bracket and I always provide the space before the ending square bracket so if you don't do it maybe you will get some problems right so always provide this space and this space for your expression right so we have written this condition which checks if the file have the right permission or not and if the file have the right permission then what we want to do is we want to echo first of all echo and what we will echo if for example type some text so type some thanks data for example and because we are going to use the cat command for this so we will say to quit we need to press control D yeah so ctrl for control be so control-d if you don't know is the command which we use to come out of the cat command okay so this message we are going to show and then in the next line we will just say cat and then whatever file name we have we will you know append this data so for appending we use double redirection you know angle brackets but if you use a single redirection angle bracket then your file will be overwritten and if you do double angle brackets then your file will be appended so whatever for text you will write you know thus text will be appended to the text the file already have okay and then comes your file name which is this name right just copy it and paste it here so this is for appending the data otherwise we will just say echo so just write echo and then the files do not have write permissions okay so you can just echoed this message right so let's see if this example works or not so let's run the script and let's see if it works or not so script and then we will give the name of the file what file doesn't exist yet so let's say I give the name of the file test which does not exist so I will press ENTER and it says test does not exist right so we need to create the file so I'm going to just touch but first of all I will show you what I have in my desktop directory so I don't have this test file already so I will just use a touch command to create this file test and then we will see the permission of it so just - LS - al - check the permission of this directory files and you can see when we create the test file this have read and write permission for the user and read and write permission for the group right so for example we will just remove the write permission from this file and we will check whether this example works properly or not right so what I'm going to do is I'm going to just write chmod for changing the permission and then I will do minus W which will remove the permission from the file which is writable permission minus W stand for writable permission and minus is for removing this permission right and then the name of the file and then press Enter and once again do LS minus L and you can see we don't have a writable permission no long right so earlier it has the right permission but right now it doesn't have the right permissions right once again we will run our script so let's run the script and then once again give the test file name and then press Enter and it says the file do not have write permission because we have already removed the write permission so it will not work right so let's give back at the write permission it can be done by using chmod plus W so plus W is for providing the write permission minus W is for removing the write permission so plus W and then once again check the permission for once again and you can see it has a write permission and lets me clear the terminal and we will run the script once again so let's run the script and then say test and now it says type some text data to quit press control D so now we are in the right you know condition here right so we can write some data here so for example let's right some data so let's write some data for example hello world and then to come out of this cat command we will press control D so I'm going to press control D and I'm out of this cat command and now what I want to check is whether in my text file I have this text or not so I will just cat this text file so just cat test and you can see we have this text there okay or otherwise we can go to the desktop folder we can see this test file and we have this data there okay once again we will run the you know script and we will see whether we can append to this text from data or not so let's do it once again so I'm going to run the script once again and now I am going to give the name of the test file and let's see what we want to write here so for example I will just write shell scripting tutorial okay and then I will press control D for coming out of this commands to control B and let's see let's see if this file is you know updated or not so you can see reload and after reload this text is appended to the earlier text right so we have learnt in this blog how we can append data to a text file and we have seen nested if condition so how to use nested if condition we have also seen in this blog so I hope you enjoyed this Blog.


Post a Comment

أحدث أقدم

Recent in Technology News