Use the content of a file as standard input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use the content of a file as standard input
# 1  
Old 05-21-2013
Use the content of a file as standard input

I want to use a content of a file as standard input to a program and dump the output to a file. However, when I try the following code:
Code:
./program < input.in > output.out

The output.out is empty. So, how can I handle this problem?
Thanks in advance!
# 2  
Old 05-21-2013
What does ./program do? What is in input.in?
# 3  
Old 05-21-2013
'program' is the name of the script I want to run. The parameters of this script is in the input.in file. After running that script, the results are dumped into output.out file.
# 4  
Old 05-21-2013
Quote:
Originally Posted by Ray Sun
I want to use a content of a file as standard input to a program and dump the output to a file. However, when I try the following code:
Code:
./program < input.in > output.out

The output.out is empty. So, how can I handle this problem?
Thanks in advance!
Have you made sure the output is going to stdout? It might be going to stderr instead. If this is the case modify your statement like this:

Code:
./program < input.in 2> output.out

or this (stdout and stderr combined):

Code:
./program < input.in > output.out 2>&1

In general a UNIX process acts like a (Y-shaped) garden hose: you put something into stdin, the process does something with this input and outputs something at stdout (usually the result of its "normal" operation) and something at stderr (usually error/diagnostic messages). You can redirect where stdout goes with "[1]>" and where stderr goes with "2>". There are also other I/O-channels which can be used, but these three (0=stdin, 1=stdout, 2=stderr) are most commonly used. I/O-descriptor "X" can be redirected with "X>" and to redirect I/O-descriptor "X" where "Y" already points to is done with "X>&Y".

I hope this helps.

bakunin
# 5  
Old 05-21-2013
Quote:
Originally Posted by Ray Sun
'program' is the name of the script I want to run. The parameters of this script is in the input.in file. After running that script, the results are dumped into output.out file.
Generalities don't help.
If ./program contains:
Code:
grep -v "="

and input.in contains:
Code:
a1=123
a2=234

then the command:
Code:
./program < input.in > output.out

will create output.out as an empty file.
If ./program contains:
Code:
cat

and input.in is not an empty file, then output.out will not be an empty file.

I repeat: What does ./program do? What is in input.in?
# 6  
Old 05-21-2013
I am sorry, I can't understand why
Code:
./program < input.in > output.out

redirects the outputs to
Code:
stderr

? And I also tried the following code:
Code:
./program `cat input.in` > output.out

This time, it works well, however, does this script take the parameters from standard input this time?
What the differences between this code and the
Code:
./program < input.in > output.out

?
# 7  
Old 05-21-2013
Your program requires command line parameters. This has nothing to do with stdin.
"program" will contain some references to $1, $2,... and those are populated when you call it like this:
Code:
./program value1 value2

That's what your experiment with cat evaluated to.

If "program" would ask you for some values after you started it you could redirect the values like in your original post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

standard input and cron

I have a program that requires the user to enter input values while it is being run for example in bash ... ... .. echo "Enter your input" read input echo $input ... ... ...I need to schedule this program with crontab, hence a problem, cronjobs run in the background, any ideas on how to... (10 Replies)
Discussion started by: walforum
10 Replies

3. UNIX for Dummies Questions & Answers

Copying and Renaming file through standard input

Hi Geeks, I am relatively new to Unix. Trying out to achive a shell script by hard learning. Here is my requirment. 1. I have to search for specified strings that are given in .csv file in the directory to find the files for matching strings in the .csv file. 2. If match is found, copy... (1 Reply)
Discussion started by: uunniixxuusseer
1 Replies

4. Shell Programming and Scripting

Reading from standard input

So, I am new to shell scripting and have a few problems. I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program... (7 Replies)
Discussion started by: Bungkai
7 Replies

5. Shell Programming and Scripting

Reading Standard Input

Hello, I am new to scripting. How do I read multiple lines from the command line? I know read reads one line, but if I have to read multiple lines, how should I do? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

6. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

7. Shell Programming and Scripting

opening a file given as standard input

Hi I am trying to write a shell script which should take the file as standard input. As file(content and name both) will change for each run. It should read the file line by line. with each line I have to perform certain operation. For example I have i file foo, it looks like /usr/doc/abc... (4 Replies)
Discussion started by: shashiprakash81
4 Replies

8. Shell Programming and Scripting

How to copy from standard input

I tried copy the output files from find command into a directory. Example, find / -name core 2>/dev/null | xargs cp???? I have known that we can use xargs to execute command lines from standard input but how to use it in this case. Or I can do something besides xargs. (2 Replies)
Discussion started by: lalelle
2 Replies

9. Shell Programming and Scripting

file content to standard output from a script

hi folks how do i output contents of file in standard output. in my script, i say x=`cat filename' echo $x below is the actual file *********** asda afdf fdf sdf dsfsdfsd fds dsfdfsdfsdg ssgd sgdg sdfsdgfsdg dgfd gsfd gs sdg sfdg s in my script, i am trying to output the... (4 Replies)
Discussion started by: bryan
4 Replies

10. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies
Login or Register to Ask a Question