Unix Scripting Help: Enter Inputs and Send Output to File

  • Thread starter Quadruple Bypass
  • Start date
  • Tags
    Unix
In summary, the conversation discusses the speaker's attempt to create a script that will run a program, enter inputs, and send the output to a file. The speaker is stuck on getting the script to enter the inputs correctly and has tried multiple combinations with no success. The expert provides two possible solutions, using a temporary file or a here document, to specify the inputs for the program. The speaker thanks the expert and confirms that the suggested solution worked.
  • #1
Quadruple Bypass
120
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
  • #2
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."
 
  • #3
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.
 
  • #4
that worked perfectly, thanks!
 

1. How do I enter inputs in a Unix script?

To enter inputs in a Unix script, you can use the read command followed by the name of the variable you want to store the input in. For example:
read input
This will prompt the user to enter an input and store it in the variable named input.

2. How do I send the output of a Unix script to a file?

To send the output of a Unix script to a file, you can use the > symbol followed by the name of the file you want to save the output in. For example:
echo "Hello world" > output.txt
This will save the output "Hello world" in the file named output.txt.

3. How do I append the output of a Unix script to an existing file?

To append the output of a Unix script to an existing file, you can use the >> symbol followed by the name of the file you want to append the output to. For example:
echo "Hello world" >> output.txt
This will append the output "Hello world" to the end of the file named output.txt.

4. How do I pass arguments to a Unix script?

To pass arguments to a Unix script, you can use the $1, $2, etc. symbols to represent the first, second, etc. argument. For example:
echo "Argument 1: $1"
If you run the script with the command ./script.sh hello, the output will be "Argument 1: hello".

5. How do I run a Unix script?

To run a Unix script, you can use the ./ command followed by the name of the script. For example:
./script.sh
This will execute the script named script.sh.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
862
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top