Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 03-27-2005, 12:45 AM   #1
LQ Newbie
Registered: Mar 2005
Posts: 3

Rep: Reputation: 0
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!
Old 03-27-2005, 01:28 AM   #2
Registered: Aug 2004
Location: MI, USA
Distribution: Fedora Core 3
Posts: 137

Rep: Reputation: 15
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.
Old 03-27-2005, 01:37 AM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
#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):
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
Old 03-27-2005, 01:57 AM   #4
LQ Newbie
Registered: Mar 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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

with the text file saying:


Old 03-27-2005, 02:33 AM   #5
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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:
$ 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.
Old 03-27-2005, 04:33 AM   #6
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Is the program in the current dir? If so,try

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

EDIT: Misstyping.
Old 03-28-2005, 07:56 AM   #7
LQ Newbie
Registered: Mar 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting help. How to launch 2+ programs simultaneously. ColKurtz Linux - Newbie 2 02-18-2005 06:14 PM
Question about scripting RitzContent Programming 18 11-23-2004 10:34 AM
Inputting Japanese dj_haruko Linux - Software 0 11-04-2004 11:10 PM
Inputting the parallel port Nerox Programming 11 09-06-2004 07:51 AM
New to Scripting question s&j Linux - General 2 06-25-2002 08:27 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration