r0skar
r0skar

Reputation: 8696

Bash Script + Read Line by Line + Ambiguos Redirect?

I have this simple script:

#!/bin/bash
cmd="file.txt"

while read line
do
    command $line > $line

done < $cmd

And this .txt file:

./cmd var1 var2 var3
./cmd var1 var2 var3
./cmd var1 var2 var3
./cmd var1 var2 var3

My goal is to read each line and execute the command, but I keep getting this error:

 line x: $line: ambiguos redirect

I am new to BASH and I have no idea what this error means and while researching it, dozens of various explanations came up. Does any1 have an idea what I could be doing wrong?

Upvotes: 0

Views: 377

Answers (1)

Tim Pote
Tim Pote

Reputation: 28029

What you're executing is essentially:

command var1 var2 var3 > var1 var2 var3

The shell can't figure out which file you want the output to redirect to: var1, var2, or var3

I'm not certain what you're trying to do, but if you want to output to, say var1, then you could do:

while read firstVar line; do
  command $firstVar $line > $firstVar
done < file.txt

However, if--as your post says--you only want to execute the command, then you don't need redirection at all. Simply do:

while read line; do
  command $line
done < file.txt

Upvotes: 2

Related Questions