BabaYaga
BabaYaga

Reputation: 529

how to enter prompt input through shell script

I have a shell script (main.sh) in which first few lines read some data through user's input.

    echo   "Enter the model  !!"
    read model
    echo "Enter the Weight !!"
    read wgt
    echo  "enter the data file  !!"
    read datafile
    echo  "Enter the two column names  !!"
    read coll1 coll2

these variables $model, $wgt, $datafile, $coll1, $coll2 are being used in the rest of the programs. When I run this by ./main.sh and give inputs respectively MODEL, WGT, DATA, COL1 COL2, everything is running fine. But I want to give these inputs through a file. So I created another script file which contains

    echo "COL1 COL2" | echo "DATA" | echo "WGT" | echo "MODEL" | ./main.sh

its only taking the first input i.e. MODEL. Is there any way to do this correctly?

Upvotes: 1

Views: 1137

Answers (3)

Jahid
Jahid

Reputation: 22428

You can do it like this:

In main script:

coll1=$1
coll2=$2
datafile=$3
wgt=$4
model=$5

Then run your main script like this:

./main col1 col2 data wgt model

You have to maintain the sequence...

Upvotes: 1

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72745

Change your main.sh to recieve these various parameters as arguments rather than on the standard input and then invoke it like so

./main.sh $COL1 $COL2 $DATA ....

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 80921

Don't pipe echo to echo. echo doesn't read standard input and so you are losing everything but the last one. Also if that worked as written it would likely be backwards.

You want something more like this:

{
    echo "MODEL"
    echo "WGT"
    echo "DATA"
    echo "COL1 COL2"
} | ./main.sh

Which, of course, could also just be:

printf 'MODEL
WGT
DATA
COL1 COL2
' | ./main.sh

Upvotes: 3

Related Questions