Updated April 10, 2023
Introduction to Shell Script Read File
In reading a shell script file we first analyze and confirm if the file has read privileges or not. In case the privilege is present, we would start reading the file. In reading there are various ways possible. Either we can read the file character by character, word by word or line by line. These are the 3 possible characteristics present in reading a file. Now it’s a subjective matter on which characteristic needs to be used in case of reading the file.
One can argue that character is better, someone can argue that word by word is better and others might stand with line. Each have their own interpretation and significance when it comes to reading. If the data in the file is huge, we would prefer going through the file line by line, in case it has a smaller number of words we can prefer going either word by word or character by character depending on the usability. In most of the common scenarios, it is preferred to read line by line.
What is Shell Script Read File?
Here we will see what kind of loops can be used for reading a file. There are eventually 2 loops through which it is possible to read a file. The concepts of loop are to implement these repetitive task through code and create powerful constructs through codes without any human intervention.
1. For loop
In the for loop, the initialization, condition check all happens in the syntax of the for loop itself.
Syntax:
for variablein $(cat <filename>); do
<commands to be executed>
done
2. Read while loop
In the while loop, if we have to read a file, we would need to use a keyword read and hence this while is known as read while loop. The main charismatic property is the ability to read line by line when you use the keyword read.
Syntax:
while read variable
do
<commands to be executed>
done <<filename>
The initialization is outside the while loop and that is the point of difference between for loop and read while loop. Both these loops are used keeping in mind the usability, and comfortableness of a developer to use any of the loops. Also one needs to make sure that an empty line is present in the end of the file.
3. Read by using IFS
IFS is nothing but an internal field separator which is used with while loop to split the words and line based on its value. IFS is mostly used when one reads a file by backslash escape omission.
How Shell Script Reads File?
There are essentially 3 different kinds of file read which is widely used in the industry.
- Read the file using a script: In this methodology we would use the filename in the script itself. This type of read is specifically done when a programmer uses built in file for execution of the code. Here, one can’t change the filename until and unless they have access to the code. This way is mainly used for reading some parameters needed for code execution already set in other file.
- Read the file by passing an argument to the script: In this methodology the filename is sent as an argument in the script itself and is mainly used when the shell script provides a flexibility to the user to execute the code on the filename provided by them. This methodology is mainly used when a script is written to perform routine or repetitive tasks.
- Reading the file by backslash escape omission: In this methodology, the code omits the backslash escape and prints the line as it is. What it eventually means is that if a line has \t \n etc. Then it will be presented as it is rather than omitting them. One would need to use -r option along with IFS or read while loop.
Example of Shell Script Read File
Here we see usability of the different methodologies.
Code
echo "Understanding File reading using For loop"
for url in $(cat sites_for_fileread.txt); do
echo $url
done
echo "Understanding File reading using read-while loop"
while read read_while_line; do
echo $read_while_line
done < sites_for_fileread.txt
echo "Understanding File reading using IFS"
while IFS= read -r read_while_line
do
echo "$read_while_line"
done < sites_for_fileread.txt
echo "Understanding File reading using argument passed through CLI"
echo "Name of the file is $1"
while read read_while_line; do
echo $read_while_line
done < $1
echo "Understanding File reading using read-while loop using -r option"
while read -r read_while_line; do
echo $read_while_line
done < sites_for_fileread.txt
Explanation:
In the above code we have put down all the methodologies we have described above. In for loop we can clearly see that any space or new line is treated the same way and appropriate treatment is given. In case we are reading with -r option, we see that “\” (backslash) is omitted.
Output:
Conclusion
As a final note, we would like to say that there are various possibilities of reading a file from its source and the method you would need to use depends entirely on the use case scenario for that problem statement.
Recommended Articles
We hope that this EDUCBA information on “Shell Script Read File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.