TheTargetmobile
TheTargetmobile

Reputation: 33

Bash: pass arguments from file line-by-line

I am reading arguments from a file, line-by-line, but each line has several arguments. The gist of the code is below

cat file.txt | while read LINE ; do
    echo -e `./foo.sh "$COUNT" "$LINE"`
done

foo.sh

#!/bin/bash
echo "$2\t$3\t$4"

file.txt

0 0 0
0 0 1
0 1 0
0 0 1

returned. Note it is not tabbed

0 0 0
0 0 1
0 1 0
0 0 1

This is simpler example of what I'm trying to do; my foo.sh is actually making a sql call using the arguments. I know that my foo.sh function works through debugging, so I've narrowed it down to the line reader. Any help with where I'm going wrong?

Upvotes: 3

Views: 3239

Answers (2)

Bill Burdick
Bill Burdick

Reputation: 975

Is there a reason you're nesting the outer command in an echo? What about something like this? I just added -e to the echo in foo.sh and took out the echo in the outer call.

cat foo.txt | while read LINE ; do
    ./foo.sh $COUNT $LINE
done

foo.sh:

#!/bin/bash
echo -e "$2\t$3\t$4"

Upvotes: 1

I think you need to do either:

  1. Unquote the variable
  2. Eval the string

So it becomes either:

echo -e `./foo.sh "$COUNT" $LINE`

or

echo -e `eval ./foo.sh "$COUNT" "$LINE"`

Otherwise bash will call foo.sh passing $LINE as a single parameter. By evaluating it explicitly, bash will first generate the final command string, and then reinterpret it, actually splitting $LINE into separate arguments.

Hope this helps =)

Upvotes: 2

Related Questions