Manual input using script

hello,

I have one script A that requires to to press "q" manually to quit the script.

I am calling script A in another script B. Is there any way by which this "giving q manually" can be done inside script. and it does not require to gve it manually when using script B.

Thanks.

script1.sh:

#! /bin/bash
echo "I am in script 1. Going to script 2"
./script2.sh
echo "I am back to script 1. Exiting now"

script2.sh:

#! /bin/bash
echo "I am in script2"
read -p "Press q to quit from script B: " input
if [ "$input" == 'q' ]
then
    echo "Exiting from script 2"
    exit
fi

Is this what you're looking for?

Hello,

Please see below for more clarity:

1.sh
2.sh

out put of 1.sh on consol is as below

 
Please run report:
2:       dailyreport
q:       quit
Choose: 

now i m calling 1.sh in 2.sh, so I want to give the option "q" automatically from script 2.sh when its asked while execution of 1.sh from 2.sh.

--B.sh--

#!/bin/bash
s2='A.sh'
echo "Calling Another Script -> $s2";sleep 1
export c="$1"
. $s2

--A.sh--

#!/bin/bash
sleep 5
echo "B script is called from $$ pid"
while [ "$c" != "q" ] ; do
read -p "Please enter choice ? [ q (for quit)]" c
done
# ./B.sh
Calling Another Script -> A.sh
B script is called from 23032 pid
Please enter choice ? [ q (for quit)]a
Please enter choice ? [ q (for quit)]d
Please enter choice ? [ q (for quit)]f
Please enter choice ? [ q (for quit)]q

if you send a parameter to B.sh and export it :wink:

# ./B.sh q
Calling Another Script -> A.sh
B script is called from 23350 pid

regards
ygemici

Thanks for the reply.

The problem is we do not have access of A.sh (as in your example). So it would not be possible to export the same variable name (as in B.sh) varibale corresponding to A.sh. :frowning:

thats what more worrying as how would I pass "q" when its required.

we must know that hold for q value which defined in "1.sh" script then maybe we define(export) to this in the "2.sh".
do you have read permission on the "1.sh" ?

I hope this would help u...

cat > B.sh
#!/bin/bash
echo "Calling Another Script -> A.sh";
./A.sh < input.txt

create an input.txt file with all options (newline separated) and last option should be q.

Another option is to use "expect".

Check this link:
-----> Expect

And if you don't know anything about "expect", check "autoexpect", it creates the "expect" script for you:
-----> autoexpect

I hope it helps!

Hi All,

Thanks a lot for your help.

siva shankar,

Your inputs worked :slight_smile: Thanks a lot !!

Regards,
Sourabh