Hemanth
Hemanth

Reputation: 161

Send automatic input to a script called by another script in bash

I'm working on a bash script (my_script) in which I call many scripts, they all together automate a work flow.

But when I call one particular (ksh/bash) script (master_script) there are many inputs and checks taken (not arguments) in it.

It is slowing down the whole of the automation, as every time I have to super wise it and enter the values manually.

I have no option to modify or make a new script (work constraints)

Every time the questions are same. I am trying to take all the answers before executing master_script except one answer(whose value depends on the execution) and then feed it to the master_script at the correct time.

Is there a way we can pass the value to the master_script, during its execution from within my_script.? ./master_script<< EOF .. EOF will not help as I have to enter one answer myself.

The below is just an example and my creation, but depicts what exactly is my requirement.

Example code

my_script

#! /bin/bash
echo "Proceeding...."
#calling master_script
/master_script $arg1 $arg2
echo "Completed.."
echo "Executing other scripts"
/other_scripts"

Execution

$ sh ./my_script
Proceeding....

Started master_script..

Press Enter to Proceed MY_INPUT

Enter username to add (eg.user123) MY_UNAME

Enter preferred uid (eg.1234) MY_UID

Do you want to bla bla..(Y/n) MY_INPUT

Please select among the following
 1.option1
 2.Option2

Selection: MY_SELECTION

Please choose which extension to use
 1.ext1
 2.ext2
 3.ext3
 4.ext4

Do you want to bla bla 2..(Y/n) MY_INPUT

Ended master script
Completed..

Executing other scripts

Requirement

#! /bin/bash
echo "Proceeding...."
# get values for master script
read -p "Proceed(Y/n):" proceed1 
read -p "Uname:" uname
read -p "Uid:" uid
read -p "bla bla (Y/n):" bla1
read -p "Selection(1/2):" selection1
read -p "bla bla 2(Y/n):" bla2
#calling master_script
./master_script $arg1 $arg2 {all_inputs}
#Silent Execution of master_script until choosing execution...
Please choose which extension to use
 1. ext1
 2. ext2
 3. ext3
 4. ext4
#Silent Execution of master_script after choosing ext and continue with other scripts
./other_scripts
echo "Completed.."

I've read about expect/send combination, but I'm unable to comprehend how to use it. Any inputs will be greatly helpful

EDIT

I am also not sure about ./master_script<< EOF ... EOF as I have to enter one answer in the middle of execution myself.

Upvotes: 1

Views: 255

Answers (1)

Edouard Thiel
Edouard Thiel

Reputation: 6248

There is a solution using here documents and redirecting the input:

./master_script "$arg1" "$arg2" << ENDINPUT
$proceed1
$uname
$uid
$bla1
$selection1
ENDINPUT

Remark 1: the final ENDINPUT must start the line, don't indent! See Man bash

Remark 2: some scripts or programs check if the input comes from an actual terminal (calling isatty()), for instance when typing a password. It is still possible to automate the entries, but it is much more tricky.

Upvotes: 1

Related Questions