Unix Scripting Help: Enter Inputs and Send Output to File

  • Thread starter Thread starter Quadruple Bypass
  • Start date Start date
  • Tags Tags
    Unix
AI Thread Summary
To create a script that runs a program, inputs data, and outputs results to a file, it's essential to manage standard input correctly. The initial attempt to use the command "./program > file" only redirects standard output, not standard input. To resolve this, two effective methods are suggested: 1. **Using a Temporary File**: Create a temporary file to store inputs, then run the program with this file as input. This involves using commands to create the temp file, write inputs to it, and execute the program while redirecting output to the desired file.2. **Using a Here Document**: This method allows embedding input directly in the script. By using a delimiter (like EOF or another chosen string), multiple lines of input can be provided directly to the program.Both methods successfully redirect standard input and output, enabling the script to function as intended. The here document approach is particularly noted for its simplicity.
Quadruple Bypass
Messages
120
Reaction score
0
hi! I am trying to create a script that will run a program, enter inputs (this program requires some inputs for it to run), and then send the output to a file. I am stuck on trying to get the script to enter in those inputs :( so far all i have is:

./program > file
<input1>
<input2>
<input3>

and unfortunately this doesn't work. I've tried like a thousand combinations that i thought would work but i can't figure it out. I am pretty sure there is something wrong with the way I am sending the outputs to the file, but i think i might be able to figure that out whereas i have no idea how to get the inputs inputted in correctly. I am new to unix as you can tell. any help is appreciated!
 
Last edited:
Technology news on Phys.org
Your program could be buggy, you might have forgotten to do chmod +x on your script, you might have forgotten to put #!/bin/bash as the first line of the script (or #!/usr/bin/python or whatever your script interpreter is), you might not have permission to write to the file, or perhaps some other problem. Obviously you should have given more information than "it doesn't work."
 
Quadruple Bypass said:
./program > file
<input1>
<input2>
<input3>

and unfortunately this doesn't work.
That works quite nicely -- just not the way you think it should. The shell script command processor (which one? tcsh? bash? something else?) processes the first line by invoking ./program in a subprocess, with stdout redirected to file. What about stdin? Standard input to your shell script is presumably your terminal -- and you have said nothing to change that.

You need to tell your shell script command processor that you want ./program run with standard input from something other than your terminal. There are many ways to do this. Two of them are to
  1. Write your data to a temporary file and run your program with this temporary file as input
  2. Run your program with data embedded in the script.


1. Using a temporary file (assumes csh, tcsh, or something like that)
Code:
set tempfile = `mktemp my_script.XXXXXX`
echo "input 1" >> $tempfile
echo "input 2" >> $tempfile
echo "input 3" >> $tempfile
./program < $tempfile > file
rm $tempfile


2. Using a here document
Code:
./program > file <<EOF
input 1
input 2
input 3
EOF
What if you want to send the string EOF to your program? Simple: Use something other than EOF to demarcate the end of the here document.
Code:
./program > file <<DONE
input 1
input 2
input 3
EOF
DONE

The here document approach is nice and simple in this case.
 
that worked perfectly, thanks!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
3
Views
2K
Replies
9
Views
1K
Replies
1
Views
2K
Replies
4
Views
685
Replies
5
Views
2K
Replies
3
Views
4K
Replies
9
Views
3K
Back
Top