Atomiklan
Atomiklan

Reputation: 5464

Bash read file into script

Ok I have a really weird question, and a lot of you are probably going to ask why.

I need to read a file into a script and immediately output back to a new file. I guess more appropriately, im streaming into a script and outputting to file.

A java process from our dev team is going to call my script (script.sh) and its going to read in a "file" and some variables.

I know you can do the following:

./script.sh var1 var2 var3

and that will allow you access those variables via $1 $2 and $3

That's great, and that how they will pass a few things to the script, but they also need to read in an XML file (but its not really a file just yet. Its just output from the java process). I think this is how it will work.

./script var1 var2 var3 < file

basically, the first thing my script needs to do is output the "file" to say file.xml, then the script will go about its merry way and start working with the variables and doing the other things it needs to do.

I assume the file being passed into the script is stdin so I tried doing things like:

0> file.xml

or

/dev/stdin > file.xml

but nothing works. I think im just making large conceptual mistakes. Can someone please help?

Thanks!

Upvotes: 0

Views: 256

Answers (1)

that other guy
that other guy

Reputation: 123670

Use cat:

cat > file.xml

With no arguments, cat reads from stdin and writes to stdout.

As for your conceptual mistake, you didn't consider that file descriptors are merely integers and can't do any work (like moving data) on their own. You need a process that can read from one and write to the other.

Another familiar process for moving data is cp, and you can indeed do cp /dev/stdin file.xml, but this is non-idiomatic and has some pitfalls.

Upvotes: 2

Related Questions