LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting question - inputting into programs (http://www.linuxquestions.org/questions/linux-newbie-8/scripting-question-inputting-into-programs-306534/)

yayphysics 03-27-2005 12:45 AM

Scripting question - inputting into programs
 
Hiya - I've just started learning linux recently, but I've come across a problem in my work that my googling hasn't been able to solve. Here's the problem:

There is a process that we have to run where we take a file and put it in a program that runs on linux. We then take the file that results and input it into another program, but that is beside the point. I need to write a linux script that ties these two together, but my problem is that the first program requires two user inputs: a number that tells the program we are going to input a filename, and then that filename.

The problem is that the program takes user inputs. That is, it waits at two points in the program for the user to input. I can take care of the first input (the number) by piping:

echo "20\n" | calculator

However, I still need to input the name of the file that will be calculated upon. I have tried replacing (echo "20\n") with (echo "20\nfile.txt\n") and (echo "file.txt\n" | echo "20\n") and other stuff (yeah, I know the last one was incorrect - I was desperate!), but none of those worked.

So, I ask if there is a way to do that with linux, or if there is a way to do it with perl, C++, or java. Thank you for your help! :D

volvogga 03-27-2005 01:28 AM

I'm not sure what you are trying to do; I'll be honest. But I believe that you may be able to do the inputting and program glueing with python. Just started to learn it myself, and my teacher told me that it is one of the main glue languages used in Linux.
From what I understand of your post, this looks to have a very high probablity of being done one way or the other. If i knew more, I almost think that it could be done in an DOS batch file.

Good Luck.

Dark_Helmet 03-27-2005 01:37 AM

I don't think you need to do anything extraordinarily fancy. Use input redirection. For example:
Code:

some_executable < some_input file
As an example/proof-of-concept, here's a pretty simplistic C program to get two user inputs (a number and a string):
Code:

// filename: simple_input.c
#include <stdio.h>

int main( int argc, char *argv[] )
{
  int number_input;
  char text_input[256];

  printf( "Enter a number: " );
  scanf( "%d", &number_input );

  printf( "Enter some string: " );
  scanf( "%s", text_input );

  printf( "You entered %d and %s\n", number_input, text_input );

  return 0;
}

Create a data file that contains this (I named it some_input.txt):
Code:

32
I_like_cheese

Compile the program (gcc -o simple_input simple_input.c), and run it like so:
Code:

./simple_input < some_input.txt
The output is kind of messy looking, but the point is, the program received the values, as evidenced by the last printf. Just have your script create a temporary file, shove what input you need into it, run the program giving it the temporary file as input, and then delete the temporary file. The /tmp filesystem was made for this :)

yayphysics 03-27-2005 01:57 AM

DarkHelmet, thanks for your reply, but I tried that and it didn't work. Unfortunately, this program I'm trying to input into is one I neither know the source code for nor even the language it was written in. The text file idea, though, doesn't work, and here is the output I get:

[folder:me]test1
Read from terminal (10) / read from file (20)
invalid number: incomprehensible list input
apparent state: unit 5 (unnamed)
last format: list io
lately reading direct formatted external IO
./test1: line 3: 23879 Aborted /programs/calculate <test1

with the text file saying:

20
input.txt

:(

Dark_Helmet 03-27-2005 02:33 AM

Well, without having the program itself to play with, I'm not sure how much more help I can be.

You can try ripping out the newline character from your echo statements:
Code:

$ echo "20 input.txt" | calculator
Other than that, I'm not sure what else you could try... Maybe the program has some command-line options you can give to suppress the user-input prompts.

enemorales 03-27-2005 04:33 AM

Is the program in the current dir? If so,try

Code:

echo "20 input.txt" | ./calculator
That works for me.

EDIT: Misstyping.

yayphysics 03-28-2005 07:56 AM

Sorry, none of these have worked. :( I'm trying to see if I can get a look at the source code for the first program... perhaps there's another way to input, though I doubt it, knowing the purpose of the program. It was never really meant to be run from a linux script and was always meant to just take the inputs from a user.


All times are GMT -5. The time now is 08:39 AM.