Reading line by line line by line from shell script

I ran into a problem in a bash shell script. This script should execute the following script shell (./script here), and the script output is redirected to a file (tmp). Then the file should be read line by line and the same script (./script) should be executed for each line, giving the line as an argument, and the result should be saved in the file (tmp1). Ultimately, these results should be added to the first file (tmp).

I embed the script below:

./script $1 $2 > tmp
cat tmp | while read a
do
 ./script $a $2 >> tmp1
done

cat tmp1 | while read line
do
 ./script $line $2 >> tmp
done

I get the following error while running the script "./script: line 11: syntax error: unexpected end of file"

Can someone help me with this?

Thank you very much in advance.

+3
source share
4 answers

script DOS/Windows. , script:

dos2unix ./script

, Windows \r (0x0d) . , dos2unix.

+3

. , ,

IFS='
'

, , , , , Perl Ruby.

+1

! . , summ_tmp - ?

#!/bin/bash
set -x
./wiki $1 $2 > tmp
while read -r a
do
    ./wiki $a $2 >> tmp1
done < summ_tmp

while read -r line
do
    ./wiki $line $2 >> tmp
done < tmp1

, , "./ script", . , , "./script". , .

set -x script (wiki . /script), .

+1
source

Alternatively, you can use xargs - this executes the command on each line of the file what exactly you want.

You can replace

cat tmp1 | while read line
do
    ./script $line $2 >> tmp
done

from

xargs <tmp1 -n1 -IXXX ./script XXX $2 >>tmp

-n1 means reading one line at a time from the input,

-IXXX means replacing XXX with the line that was read - by default, add it to the end of the command line.

0
source

Source: https://habr.com/ru/post/1731336/


All Articles