Operating System - Linux
1827810 Members
1924 Online
109969 Solutions
New Discussion

[ How to make script to read inputs from a file ]

 
SOLVED
Go to solution
madhavabk
Regular Advisor

[ How to make script to read inputs from a file ]

Hi all,

I have script which has no support for inputs from a file. But i use script with same values all the time.
Hence i would like to put all my answers for all queries of script in a file and want to supply it.

What is the best way to do this, with minimum changes. I would like to know is it possible to close file handle and duplicate stdin for my file.

Thanks,
Madhava
14 REPLIES 14
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: [ How to make script to read inputs from a file ]

Couldn't be simpler:

myscript.sh < myinputfile.txt
If it ain't broke, I can fix that.
Rajeev  Shukla
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Use while loop

while read i
do
myscript $i
done
madhavabk
Regular Advisor

Re: [ How to make script to read inputs from a file ]

Hi Clay Stephenson,

If i redirect input from a file will the script understand the format or we need to take care of checking each entry?

My doubt is if i put all responses on seperate lines will script parses it correctly and gives inputs to queries in script.

Thanks,
Madhava
Arunvijai_4
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Yes, You can do that, shell reads line by line.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
madhavabk
Regular Advisor

Re: [ How to make script to read inputs from a file ]

Hi,

I am getting problem with script during only password reading portion.

Other than script is able to read line by line for each input required.

Thanks,
Madhava
Arunvijai_4
Honored Contributor

Re: [ How to make script to read inputs from a file ]

If possible, can you post your script ? We shall take a look at it and modify .. What kind of problem you are facing in password reading portion ?

-Arun

"A ship in the harbor is safe, but that is not what ships are built for"
RAC_1
Honored Contributor

Re: [ How to make script to read inputs from a file ]

what password it asks?? Is that system related?? You may use expect tool for it.
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: [ How to make script to read inputs from a file ]

A simplest way is as,

--- configuration.ksh ----
name=madhavbk
day="Oct 5"

--- script.ksh ---
. ./configuration.ksh
echo $name
echo $day


.

It is like a header file inclusion in ksh scripting.

hth.
Easy to suggest when don't know about the problem!
madhavabk
Regular Advisor

Re: [ How to make script to read inputs from a file ]

Hi all,

For all password requests when there is echo off for stty script is not reading password from file.
All other entries it is taking correctly.

Any clues.

Following is the error message.

stty: : Not a typewriter

Thanks,
Madhava
Muthukumar_5
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Refer this:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=891850

hth.
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Can you check this doc,
http://docs.hp.com/en/B2355-90046/ch21s04.html
[Shells: User's Guide > Chapter 21. Basic Shell Programming Data Input and Output]

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=944865
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=35433

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Are you running this script in batch mode with /etc/profile or .profile. So that it will make that. Try to add that script with profile itself. Refer more,

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=72142

hth.
Easy to suggest when don't know about the problem!
RAC_1
Honored Contributor

Re: [ How to make script to read inputs from a file ]

Just before stty -echo statement, you may put this code and try.

The error is because, it expacts ainteractive terminal and it does not get one.

if tty -s
then
INTERACTIVE=/sbin/true
else
INTERACTIVE=/sbin/false
fi
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: [ How to make script to read inputs from a file ]

It's those little details you fail to mention...

Reading passwords really has nothing to do with echo off or on. It depends on which passwords you are trying to read and what routines are actually being used but in almost all cases, stdin is not the source of input for the password read. Those are almost always read using another file descriptor. In general, you must use a tool like expect (which does handle multiple file descriptors) to process complicated password entry. You can get expect from any of the HP-UX Porting Centre's.
If it ain't broke, I can fix that.