3

I'm writing a bash script called 'run' that tests programs with pre-defined inputs.

It takes in a file as the first parameter, then a program as a second parameter.

The call would look like

./run text.txt ./check

for example, the program 'run' would run 'check' with text.txt as the input. This will save me lots of testing time with my programs.

right now I have

$2 < text.txt > text.created

So it takes the text.txt and redirects it as input in to the program specified, which is the second argument. Then dumps the result in text.created.

I have the input in text.txt and I know what the output should look like, but when I cat text.created, it's empty.

Does anybody know the proper way to run a program with a file as the input? This seems intuitive to me, but could there be something wrong with the 'check' program rather than what I'm doing in the 'run' script?

Thanks! Any help is always appreciated!

EDIT: the file text.txt contains multiple lines of files that each have an input for the program 'check'.

That is, text.txt could contain

asdf1.txt asdf2.txt asdf3.txt

I want to test check with each file asdf1.txt, asdf2.txt, asdf3.txt.

7
  • What you've got is the proper way to run a program with text.txt as the standard input. Are you sure that this is what the program expects? (Different programs have different expectations, don'tch'ya know?) Commented May 21, 2012 at 20:32
  • @dmckee sweet! Well text.txt contains "lets go" without the quotes and 'check' is expecting two parameters. Weird that nothing is in text.created Commented May 21, 2012 at 20:36
  • Ah...redirecting a file to the standard input is emphatically not the same as using the file's contents for arguments. Answer to follow. Commented May 21, 2012 at 20:37
  • I have revised my answer - now it uses parameters, not standard input Commented May 21, 2012 at 20:42
  • @dmckee I've added a few edits that might change things a little. :) Commented May 21, 2012 at 20:42

2 Answers 2

3

The < operator redirects the contents of the file to the standard input of the program. This is not the same as using the file's contents for the arguments of the file--which seems to be what you want. For that do

./program $(cat file.txt) 

in bash (or in plain old /bin/sh, use

./program `cat file.txt`

).

This won't manage multiple lines as separate invocations, which your edit indicates is desired. For that you probably going to what some kind scripting language (perl, awk, python...) which makes parsing a file linewise easy.

Sign up to request clarification or add additional context in comments.

3 Comments

Would $2 < cat filename > filename.created work? I got the filename through a loop I run through text.txt. Just need to feed it in check.
No, $() is the "evaluate contents" operator and is the way to evaluate a string and use it in situ. You may want ./program $(cat filename) > craeted.txt.
backtick FTW!! Looked all over the place for this but couldn't find it. I was erroneously trying normal tick marks (') and it wasn't working. Thanks!
2

A simple test with

#!/bin/sh

# the whole loop reads $1 line by line
while read
do
    # run $2 with the contents of the file that is in the line just read
    xargs < $REPLY $2
done < $1

works fine. Call that file "run" and run it with

./run text.txt ./check

I get the program ./check executed with text.txt as the parameters. Don't forget to chmod +x run to make it executable.

This is the sample check program that I use:

#!/bin/sh

echo "This is check with parameters $1 and $2"

Which prints the given parameters.

My file text.txt is:

textfile1.txt
textfile2.txt
textfile3.txt
textfile4.txt

and the files textfile1.txt, ... contain one line each for every instance of "check", for example:

lets go

or

one two

The output:

$ ./run text.txt ./check
This is check with parameters lets and go
This is check with parameters one and two
This is check with parameters uno and dos
This is check with parameters eins and zwei

1 Comment

Thanks! This is useful stuff, quick question though, what if text.txt contained the filenames of other inputs for check? for example, asdf1.txt which contained lets go asdf2.txt which contained one two

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.