0

In a shell script, if I specify directly the text file, it works:

#!/bin/bash

IFS=''
while read input <&3 && read output <&4; do     
    echo "$input -----> $output"
done 3<inputfile.txt 4<outputfile.txt #This line works

However, when I try to make the script accept arguments (./myscript.sh inputfile.txt outputfile.txt), there's nothing echoed:

#!/bin/bash

fromtxt="$1"
totext="$2"
IFS=''
while read input <&3 && read output <&4; do     
    echo "$input -----> $output"
done 3<"$fromtxt" 4<"$totext" #This line doesn't

What's my mistake?

Gregor Isack
  • 1,111
  • 12
  • 25
  • 1
    It works when I test it. I'd recommend adding `set -x` at the beginning, so it'll print the commands as it executes them and you can get some idea what's happening. – Gordon Davisson May 19 '20 at 02:53
  • I didn't realize about `set -x` until today, thank you. It seems like I'm having missing last line issue https://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line. – Gregor Isack May 19 '20 at 03:20
  • 1
    Hmm. The order of precedence of `||` and `&&` isn't going to be helpful in combining that with what you have. Try `while { read input <&3 || [ -n "$input" ]; } && { read output <&4 || [ -n "$output" ]; }; do` – Gordon Davisson May 19 '20 at 04:51

1 Answers1

0

Perhaps you can avoid looping with awk or with a solution like

paste -d "\r" inputfile.txt outputfile.txt| sed 's/\r/ ------> /'
Walter A
  • 19,067
  • 2
  • 23
  • 43