Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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!
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.
I don't think you need to do anything extraordinarily fancy. Use input redirection. For example:
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):
// filename: simple_input.c
int main( int argc, char *argv )
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 );
Create a data file that contains this (I named it some_input.txt):
Compile the program (gcc -o simple_input simple_input.c), and run it like so:
./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
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:
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
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.