Yra daug būdų, kuriais galime skaityti failą „Bash Shell Scripting“. Kai kurie svarbūs metodai pateikiami toliau (darant prielaidą, kad failo, kurį skaitome, pavadinimas yra „read_file.txt“):
Failo skaitymas naudojant „cat fileName“
Galime naudoti šią sintaksę, kad galėtume atspausdinti failo turinį į terminalą.
value=`cat file_name`
Pavyzdys
#!/bin/bash value=`cat read_file.txt` echo '$value'
Išvestis
Failo skaitymas naudojant '$()
Toliau pateikiama failo turinio skaitymo sintaksė naudojant „$“
value=$(file_name)
Pavyzdys
#!/bin/bash value=$(<read_file.txt) echo '$value' < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-2.webp" alt="Bash Read File"> <h3>Reading File Content from Command-line</h3> <p>If we want to read a file line by line from commands-line without using the 'cat' command, we can run the following command to perform a task:</p> <p> <strong>Command</strong> </p> <pre> while read line; do Command; done <input.file < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-3.webp" alt="Bash Read File"> <p>Here, while loop will reach each line of the file and store the content of the line in $line variable which will be printed later.</p> <h3>Reading File Content Using Script</h3> <p>To read the file content using the script, we need to create a bash file and add the following code:</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash file='read_file.txt' i=1 while read line; do #Reading each line echo 'Line No. $i : $line' i=$((i+1)) done <$file < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-4.webp" alt="Bash Read File"> <p>Here, an existing filename is stored in $file variable, and $i variable is used to keep the value of the line number of that line.</p> <h3>Passing filename from Command line and reading the File</h3> <p>Create a bash and add the following script which will pass filename from the command line and read the file line by line. The first argument value is read by the variable $1, which will include the filename for reading. If the file is available in the specified location then while loop will read the file line by line and print the file content.</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash file=$1 while read line; do #Readind each line in sequence echo $line done <read_file.txt < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-5.webp" alt="Bash Read File"> <p>Here, the filename is used as an argument value. The output will provide the content of 'read_file.txt' with no extra spaces between words.</p> <h3>Reading file by omitting Backslash Escape</h3> <p>If we want to read each line of a file line by line by omitting backslash-escape then we are required to use the '-r' option with a 'read' command in 'while' loop, e.g.:</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash while read -r line; do #Reading each line by omitting backslash escape echo $line done <read_file.txt < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-6.webp" alt="Bash Read File"> <p>We may require reading the file for several programming purposes. For example, we can search or match any specific content easily from the file line by line. Hence, it is a useful task for any programming language.</p> <hr></read_file.txt></pre></read_file.txt></pre></$file></pre></input.file></pre></read_file.txt)>$file>