|
From: john on 1 Aug 2006 11:15 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 1 Aug 2006 11:24 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 1 Aug 2006 11:28 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 1 Aug 2006 14:02 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 1 Aug 2006 14:03 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
|
Pages: 1 Prev: "setenv EDITOR vi" don't work Next: convert .doc to ascii |