Most of the times, shell script is used to interact with files.
File Example 1:
Below is a program to check if a file exists or not
In this example, we take file name input from the user. Note the “\c” option, this is used to keep the cursor in the same line. Then we use “-e” option to check if the user entered file is present or not. This will check the presence of the file in the same directory.
#!/bin/bash echo -e "Enter the file name \c" read file_name if [ -e $file_name ] then echo "File found" else echo "File not found" fi
Output:
sh file.sh Enter the file name hello.txt File not found
As initially the file is not present, it will display file not found.
We shall create a file by using touch command, then run the program again.
touch hello.txt sh file.sh Enter the file name hello.txt File found
File Example 2:
“-f” option is used to check if the file exist and it is a regular file or not.
#!/bin/bash echo -e "Enter the file name \c" read file_name if [ -f $file_name ] then echo "File found and is a regular file" else echo "File not found" fi
Output:
sh file.sh Enter the file name hello.txt File found and is a regular file
File Example 3:
“-d” option is used to check if the file exist and it is a directory file or not.
#!/bin/bash echo -e "Enter the file name \c" read file_name if [ -d $file_name ] then echo "File found and is a directory file" else echo "File not found" fi
Output:
sh file.sh Enter the file name mydir File found and is a directory file
Similarly
“-b” to check block special file.
“-c” to check character special file.
“-s” to check file is empty or not.
“-r” tp check if the file has read permission
“-w” tp check if the file has write permission
“-x” tp check if the file has execute permission
File Example 4:
In this example, we shall open a file, check if we have write permission and write something into that file. To write into the file use “cat” command.
#!/bin/bash echo -e "Enter the file name \c" read file_name if [ -f $file_name ] then echo "File found" if [ -w $file_name ] then echo "File is having write permission. Write text below. Ctrl + cat >> $file_name fi else echo "File not found" fi
Output:
sh file.sh Enter the file name hello.txt File found File is having write permission. Write text below. Ctrl + D to exit Hello world from prodevelopertutorial.com
Now the text “Hello world from prodevelopertutorial.com” will be written in “hello.txt” file.
File Example 5:
In this example we shall look how to read files using while loop:
Using “read” command.
Using read command, we shall use read command along with a variable, then echo that variable to output. We use “<” input redirection to input the file.
#!/bin/bash echo "Reading the file hello.txt" while read r do echo $r done < hello.txt
Output:
sh 2_file.sh Reading the file hello.txt Hello world from prodevelopertutorial.com
Using “read” and pipe command.
In the previous example, we used “<” command. In this example we use “|” command to input file name.
#!/bin/bash echo "Reading the file hello.txt" cat hello.txt | while read r do echo $r done
Using “IFS” and pipe command.
If the file has special characters like file indentation or a space or a tab or a newline, those values cannot be ready by the above methods. Hence at that place we use “IFS” keyword. IFS stands for Internal Field Separator.
#!/bin/bash echo "Reading the file /etc/resolv.conf" while IFS= read -r line do echo $line done < /etc/resolv.conf
Output:
sh 2_file.sh Reading the file /etc/resolv.conf # Generated by dhcpcd # /etc/resolv.conf.head can replace this line # /etc/resolv.conf.tail can replace this line