How to read while reading from a file in bash

mehdi

i want to use read cmd in a while that read line per line from a file, but i have a problem in line 4 in this script :

while read a
do
    echo before
    read var
    echo after
done < file1

the result is :

before
after.

Can you help me to fix this problem????

kojiro

A file descriptor can really only point to one file at a time. Every invocation of read you have there reads from standard input, including the one in the middle. If you want to read from something else, you have to tell read to use another file descriptor.

exec 5< file1 # assign file1 to the file descriptor number 5.
              # ("open for reading" as it were)
while read <&5 a; do # read from fd5
  echo before
  read var # read from fd0
  echo after
done
exec 5<&- # Reset fd 5 ("close the file" as it were)

You can also use read -u 5 to read from a specific descriptor in bash.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

First character not read while reading file from server

From Dev

Reading lines from a file with bash: for vs. while

From Dev

Reading lines from a file with bash: for vs. while

From Dev

How to read from a file, and then continue reading from cin?

From Dev

How to read from file *and* stdin in bash

From Dev

How to read variables from a php file in bash

From Dev

How to read coordinates from text file in bash?

From Dev

Bash: Why is read returning a nonzero exit status while reading my file?

From Dev

Bash script - how to read file line by line while using getopts

From Dev

Bash: How to read file in while loop containing multiple conditions?

From Dev

how to change datatype of a column while reading from a csv file

From Dev

How to detect a delimiter while reading from a socket file descriptor in C?

From Dev

How to avoid unsupported .nc file while reading from different directory

From Dev

How to skip over white spaces in a string while reading from a file

From Dev

How to skip primitive data values while reading from file

From Dev

How to use 2 delimitters while reading from a file in PHP

From Dev

How to "save the state" when reading from a file with `read`?

From Dev

bash: Prompting for user input while reading file

From Dev

Issue while reading a file from WAR file

From Dev

Error while reading compressed CSV file from URL with pandas.read_csv

From Dev

Bash: reading input within while read loop doesn't work

From Dev

Using a while loop to read a file, randomly stops reading the file in the middle

From Dev

File read and write while reading the file line by line

From Dev

Qt: How to lock/prevent a file from being read while it is written?

From Dev

How to read data from file while running the docker image

From Dev

How show progress bar while read file from SD card

From Dev

How to use `while read -r line` while also checking if another file is not empty in BASH?

From Dev

bash: nested while read & for file in - no output

From Dev

Exception while reading file from SD Card

Related Related

  1. 1

    First character not read while reading file from server

  2. 2

    Reading lines from a file with bash: for vs. while

  3. 3

    Reading lines from a file with bash: for vs. while

  4. 4

    How to read from a file, and then continue reading from cin?

  5. 5

    How to read from file *and* stdin in bash

  6. 6

    How to read variables from a php file in bash

  7. 7

    How to read coordinates from text file in bash?

  8. 8

    Bash: Why is read returning a nonzero exit status while reading my file?

  9. 9

    Bash script - how to read file line by line while using getopts

  10. 10

    Bash: How to read file in while loop containing multiple conditions?

  11. 11

    how to change datatype of a column while reading from a csv file

  12. 12

    How to detect a delimiter while reading from a socket file descriptor in C?

  13. 13

    How to avoid unsupported .nc file while reading from different directory

  14. 14

    How to skip over white spaces in a string while reading from a file

  15. 15

    How to skip primitive data values while reading from file

  16. 16

    How to use 2 delimitters while reading from a file in PHP

  17. 17

    How to "save the state" when reading from a file with `read`?

  18. 18

    bash: Prompting for user input while reading file

  19. 19

    Issue while reading a file from WAR file

  20. 20

    Error while reading compressed CSV file from URL with pandas.read_csv

  21. 21

    Bash: reading input within while read loop doesn't work

  22. 22

    Using a while loop to read a file, randomly stops reading the file in the middle

  23. 23

    File read and write while reading the file line by line

  24. 24

    Qt: How to lock/prevent a file from being read while it is written?

  25. 25

    How to read data from file while running the docker image

  26. 26

    How show progress bar while read file from SD card

  27. 27

    How to use `while read -r line` while also checking if another file is not empty in BASH?

  28. 28

    bash: nested while read & for file in - no output

  29. 29

    Exception while reading file from SD Card

HotTag

Archive