From: john on
I have a simple C program that does SCANF on a file that contains
numbers, then prints it out. Here is how I run it:
a.out < matrix.txt

If I put that command in a script, it works fine, but if this is my
script, it doesn't get the redirection:
#!/bin/sh
OPTION="< matrix.txt"
a.out ${OPTION}

So stdin just sits & waits for keyboard input.

What am I doing wrong? Thanks!

From: dfeustel on
john <brgr88(a)yahoo.com> wrote:
> I have a simple C program that does SCANF on a file that contains
> numbers, then prints it out. Here is how I run it:
> a.out < matrix.txt
>
> If I put that command in a script, it works fine, but if this is my
> script, it doesn't get the redirection:
> #!/bin/sh
> OPTION="< matrix.txt"
> a.out ${OPTION}
>
> So stdin just sits & waits for keyboard input.
>
> What am I doing wrong? Thanks!

Try

#!/bin/sh
OPTION="matrix.txt"
a.out < ${OPTION}


--
Using OpenBSD with or without X & KDE?
http://dfeustel.home.mindspring.com
From: Thomas J. on
john schrieb:

> #!/bin/sh
> OPTION="< matrix.txt"
> a.out ${OPTION}

try:

eval a.out ${OPTION}

hope this helps

Thomas

btw. $OPTION does not contain an option... ;)

From: Chris Mattern on
john wrote:
> I have a simple C program that does SCANF on a file that contains
> numbers, then prints it out. Here is how I run it:
> a.out < matrix.txt
>
> If I put that command in a script, it works fine, but if this is my
> script, it doesn't get the redirection:
> #!/bin/sh
> OPTION="< matrix.txt"
> a.out ${OPTION}
>
> So stdin just sits & waits for keyboard input.
>
> What am I doing wrong? Thanks!
>
The shell does things in a certain order. It scans the line
for I/O redirection characters *first*. Only after that does
it do variable substitution. So by the time it's translated
$OPTION to "< matrix.txt" it's too late. It simply passes "<"
and "matrix.txt" to your program as parameters, where
you could retrieve them with argv[] if you were so inclined.

dfeustel's answer works because it moves the "<" out of the
variable, where the shell will find it.

Thomas's answer works because "eval" makes the the shell
parse the string once, and *then* send the string off
for execution (where it gets parsed a second time).
The "eval" parse does the variable substitution, so
the "<" is all ready there and ready to be processed
when execution of the command starts.


Chris Mattern
From: Chris Mattern on
Thomas J. wrote:
> john schrieb:
>
>
>>#!/bin/sh
>>OPTION="< matrix.txt"
>>a.out ${OPTION}
>
>
> try:
>
> eval a.out ${OPTION}
>
> hope this helps
>
> Thomas
>
> btw. $OPTION does not contain an option... ;)
>

Actually, the way he does it, it does :-). "<"
and "matrix.txt" are waiting right there in
argv[].


Chris Mattern