How to pass arguments to an interactive script

Hey guys,

I have an interactive script that is quite critical to our production environmentl thus updating it to run non-interactively is not an option.

The script takes a varying number of arguments, which it ques untill user confirm end of data entry

e.g of user input [arguments]:

1
2
y            #add another arguments set
3
4
n            #this is last set of arguments
y            #start processing

the arguments to be passed to the script are in a file. say file.in

i tried using a here-document, but it turns out that you can't put a here-document in a file as i kept getting "terminator not found" error message from ksh
i wanted to run the script like this:
script << here_doc

where here_doc had:

$
arguments
$

it didn't work! as explained above.

now the funny part, if i pass the arguments through a pipe to the script standard input, it works when using echo or print to print the arguments to standard output:

i.e. [Korn shell]

echo "1\n2\nn\ny" | myscript

^^ this works perfectly, my script runs and reads its input from the pipe. print also works as above

but my problem is, i have a file of these arguments that i want the script to sequentially read its input from.

if I use cat, it doesn't not work!!! the script reads NULL in the first variable!!

cat file.in | myscript

the same also happens when I do
myscript < file.in #script reads NULL in first variable

by the way, the script that is trying to run this script is on another machine so it ssh-es to the host machine and run the script. But i don't think this has anything to do with all this.
e.g.

ssh me@remotehost 'myscript'

Can anybody please suggest something to get myscript to read args from a file?

Thanks in advance

Hi,

you can pass file name as a argument to Your shell script like this

$ script.sh filename

in shell script arguments are stored into special variables i.e first argument is stored into $1,second in $2 .....if you want to get all arguments in one go use $*.

so inside the script use :

$ filename=$1 #to get the file name.

Then use for loop to get the contents of file like this..

for i in `cat $filename`
do
firstline=$i
your logic..........

done

Note : if the file location is differ then script location the use relative path name as :
$ script.sh /home/rajiv/filename

I am new to the forum so I was browsing some of the old threads, I noticed this post and unless this works differently in the shell you are using the above quote is incorrect. Maybe it was just an oversight.

$0 refers to the name of the command itself, $* will provide all arguments at once. See below:

$ ./foo.sh 1 2 3 4 5
This was the dollar 1: 1
This was the dollar 2: 2
This was the dollar 3: 3
This was the dollar *: 1 2 3 4 5
This was the dollar 0: ./foo.sh

$ cat foo.sh
#!/bin/sh
echo "This was the dollar 1: $1"
echo "This was the dollar 2: $2"
echo "This was the dollar 3: $3"
echo "This was the dollar *: $*"
echo "This was the dollar 0: $0"

sorry , it was a mistake.