Shell scripting tutorial no.6 - file test operators


hey guys welcome to the next blog on shelf scripting tutorial for beginners in the last blog we have seen how to use if statement else statement and if-else statement in shell script now in this blog we will use those if statements to check for the file test operators so let's get started so for example or most of the times when you are writing shell script you are dealing with the files and for example if you want to check whether file exists or not or if you want want to check whether this file is a regular file or its block special file or it's a character special file or many other options if you want to check you can use these file test operator to check for those you know conditions so let's see how we can use those conditions so what I'm going to do is first of all I am going to echo statement for example I will say enter the name of the file and for you know printing the input on the same line I am going to use backslash c and this backslash c is used to keep the cursor nor cursor on the same line after the end of the echo now in order to interpret this backslash c you need to use the flag - e here now - e flag is used to you know enable the interpretation of this backslash so otherwise if you don't use - e is going to print this backslash 3 literally but if you use - es with this statement then it's going to interpret this backslash and then it is going to you know keep the cursor on the same line as the echo command line right so write this and then we are going to use the read command to read and then we to read the filename to just write file name for example and then we are going to use the if statement so just use if and then for if statement what we use we use the square brackets and then in the square brackets we will use a flag for example if we want to check whether the file exists or not so there is a special flag called minus e and then the file name so the file name is in the file name variable so after dollar just use this right and then we use then and then we need to use the echo command for example so we will just say if the file exists we will print first of all the file name so let's say this is the file name and then we will just say file found right otherwise in the else condition we will just write here in the else condition else and then we will just say file not found right so same line and then we will say not found not found right and then we will end the line by and the if statement by fi okay so let's run this script and let's see what happens so I am going to just run on my script and then press Enter let's ask me enter the name of the file and you can see the cursor is on the same line which is due to this backslash and - 'flag right so I'm going to give the name of the file which doesn't exist for example ABC or some some random file name okay and then press Enter it says this file named not found okay now let me create a file so I am going to use a touch command for this so touch for example I'm going to name my file as test for example okay and then plus enter which is going to create the stat file and then I'm going to run the script once again and then I'm going to write the name of the file which is a test which I have created and then press Enter and now it says test found which is the test file which I have created okay so this - e flag is used to check whether the file exists or not okay now there are some other options also which we can use so for example to check whether the file you know if it's a file or not you can just use a flag called F okay so E is for if the file exists or not f is the is for the file exists and if it's a regular file or not okay so let's use F and let's run the script once again and once again we will just say test and then press ENTER and save the test found because we have created the regular file using the touch command right so F flag is for checking whether file exists and if it's a regular file or not right now if you want to check for the directories you can use the flag D okay so let's so you know run the script once again and even if it's saying you know enter the name of the file we are going to enter the name of the directory for example first of all the directory name which doesn't exist so dir which does not exist on my you know folder or directory and then press ENTER and it says dir not found and let's create our dir directory for example so for this we will use MK dir command and then the name of the directory for example dir once again okay so this directory is created let's verify so you can see the directory dir is created right so let's run the script once again and then once again give the name of the directory and then press ENTER and it says that we found so - D flag is for checking whether the directory exists or not so it's worth checking the directories now there are some other flags which you can use for example there are two types of file basically one is a block special file and other is a character special file so a character special file is a normal file which contains some text or data or something like this a block special file is a binary file for example or a picture file or a video file it's a block special file so a file which contains normal text or normal character it's a character special file and other files for example video files or music files or you know image files are block special files right so for block special files you can use the flag B and for characters special file you can use the file C okay so for block special B and for character special the flags fee now there is a flag which checks whether the file is empty or not so for that you just need to use a flag called - s and it checks whether our file is empty or not so let's check so I'm going to run the script once again and then I'm going to give the name of the test file which we have created and then press Enter and it says file not found and here we just need to write for example file not empty and here we need to write file empty okay so once again let's run the test and we will give the file name test which is empty and save text d right so we can verify this by using LS - L command for example and you can see our test file is of size zero you can see here right now for example I will add some text to the file for example I will just use cat and then I will use the cat text to the test file right and then press Enter and I will write something else and then I'm going to press ENTER and for exiting out of the cat command you use control D and then once again let's do LS - L and you can see now it has some data this test file has some data so let's run the script once again and use the file name and now it says test not empty right so - f is for checking whether file is empty or not now there are some other flags also for example if you want to check whether your file has the read permission then you can use the - our flag if you want to check whether your file has the right permission you use the - W and if you want to check whether your file has the execute permission then you can use the X flag for this okay so these are some of the options which you can use to check for your file using if statement for example okay so I hope you enjoyed this blog


Post a Comment

Previous Post Next Post

Recent in Technology News