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
# 8  
Old 05-21-2013
Quote:
Originally Posted by Don Cragun
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?
program file contains:
echo $2 $1
input.in file contains:
a b

When doing:
Code:
./program < input.in > output.out

The output.out was empty
However, when doing:
./program `cat input.in` > output.out
The output.out has the expected output which was b a .

I feel so confused about that. What is the difference?
# 9  
Old 05-21-2013
Quote:
Originally Posted by Ray Sun
I feel so confused about that. What is the difference?

Quite simple: "$1" (2, 3, ...) are variables which are filled with COMMANDLINE ARGUMENTS, whereas what you are redirecting to/from is INPUT and OUTPUT respectively. These are simply two different things. If you tell someone to count all persons coming through door 1 and there will be 100 persons coming through door 2 he will still come up with 0.

What your program does is: take the first two arguments and print them to "stdout" in reversed order. Nothing at all is done with what comes from "stdin". Therefore the output file is empty: you passed no commandline arguments, therefore "$1" and "$2" are empty and this emptyness is written to the file "output.out".

Why your second try worked is simple: "cat input.in" is a useless use of cat, but will print the contents of input.in at the commandline, because it is surrounded by backticks (which should not be used either). Therefore what you wrote was equivalent to

Code:
./program a b > output.out

because this is what the shell will evaluate the whole backticks-construction to. This will fill the commandline variables "$1" and "$2" with some value ("a" and "b") and therefore something gets printed to "output.out" - just as i said above.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 10  
Old 05-21-2013
Wow, that's excellent! Now I understand the differences. But, how should I modify my program to get the standard input from input.in?
# 11  
Old 05-21-2013
Quote:
Originally Posted by Ray Sun
Wow, that's excellent! Now I understand the differences. But, how should I modify my program to get the standard input from input.in?
Change the contents of program from:
Code:
echo $2 $1

to:
Code:
read a1 a2 junk; echo $a2 $a1

or, if the contents of input.in could contain characters with special meaning to the shell (including backslash (\) or starting with a minus sign (-):
IFS="" read -r a1 a2 junk; printf "%s %s\n" "$a2" "$a1"
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 05-21-2013
Quote:
Originally Posted by Ray Sun
Wow, that's excellent! Now I understand the differences. But, how should I modify my program to get the standard input from input.in?
Will this work for you?

Content of program:
awk '{print $2" " $1}'

then you execute it like..

./program < input.in > output

This will output as..
Code:
cat output
b a

This User Gave Thanks to juzz4fun For This 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