Unix Scripting Help: Enter Inputs and Send Output to File

  • Thread starter Thread starter Quadruple Bypass
  • Start date Start date
  • Tags Tags
    Unix
Click For Summary

Discussion Overview

The discussion revolves around creating a Unix script that runs a program requiring user inputs and redirects the output to a file. Participants explore methods for providing inputs to the program within the script and address issues encountered by a newcomer to Unix scripting.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their attempt to run a program and redirect its output to a file but struggles with inputting the required data.
  • Another participant suggests several potential issues, including script permissions and the need for a proper shebang line.
  • A different participant explains how the shell script processes input and suggests using a temporary file or a here document to provide inputs to the program.
  • Examples of both methods (temporary file and here document) are provided to illustrate how to correctly input data into the program.
  • A later reply confirms that one of the suggested methods worked successfully for the original poster.

Areas of Agreement / Disagreement

Participants generally agree on the methods to input data into the program, with one method confirmed to work effectively. However, there is no explicit consensus on the best approach, as multiple methods are presented.

Contextual Notes

Some limitations include the lack of information about the specific shell being used and the original poster's level of experience with Unix scripting, which may affect the applicability of the suggested solutions.

Who May Find This Useful

Individuals new to Unix scripting, those seeking to automate program inputs and outputs, and users interested in shell scripting techniques may find this discussion beneficial.

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!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
19
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K