Writing Shell script to interactively control another shell script??

Writing Shell script to interactively control another shell script??

Post by nospamplss » Mon, 15 Jan 2007 12:04:07


Using Solaris,
Say you want to write a shell script to control another simple menu
entry shell script like the following testscript.sh file
///////////////////////
themenu()
{
echo "MENU"
echo "1 - Check Status and other stuff "
echo "2 - Run program"
echo "3 - Exit"
echo ""
}

while true
do
themenu
read input
case $input
1) //check status and do some other stuff ;;
2) //start program and other background processes;;
3) break;; //exit loop and program
*) ;;

done
//////////////////////////////////////////////////////////////////

What I want to automate is do the following

Press 1 to check status then do a grep "Program is running fine"
if "Program is running fine" then Press 3 to exit
else
Press 2 to start program then press 3 to exit

No I don,t want to use Expect or tcl so can I do this using shell
script only with out modifying the original shell script???

I could create a shell script that has the following which just starts
the program/background processes and exit but it does not check the
status beforehand.....and redirection only allows you to
send input from a file and read the output from file after the program
ends......

sh testscript.sh<<EOF
2
3
EOF

If you need clarifications on my question pls ask
Any help would be much appreciated. Thanks
 
 
 

Writing Shell script to interactively control another shell script??

Post by Janis Papa » Mon, 15 Jan 2007 12:29:23


I am not sure I understood what you want (and you didn't tell us which
shell you need to use), but...

You may want to inspect ksh's co-process facilities. It allows you to
start a program in the background

program args |&

that is still attached to the main script through a bidirectional pipe
that you can write to and read from with

print -p whatever your program expects
read -p some data returned

By re-assigning (re-directing) the co-process filedescriptors you may
use more than one co-process in your script, if necessary.

BTW, ksh also supports menus if you like (see 'select' command).

Janis

 
 
 

Writing Shell script to interactively control another shell script??

Post by nospamplss » Tue, 16 Jan 2007 23:23:14

Thanks just what I needed,
script.sh |&
exec 4>&p
10: exec 5<&p
11:
12: cat namefile
13:
14: # read the names from a file "namefile"
15: while read uname; do
16: print -u4 $uname
17: read -u5 dndname

Just another question

How do I redirect the output for the coprocess to a file??
I tried using exec 5>"test.log" new to this stuff.......but does not
work
any sample code would be helpful.
 
 
 

Writing Shell script to interactively control another shell script??

Post by nospamplss » Tue, 16 Jan 2007 23:23:17

Thanks just what I needed,
script.sh |&
exec 4>&p
10: exec 5<&p
11:
12: cat namefile
13:
14: # read the names from a file "namefile"
15: while read uname; do
16: print -u4 $uname
17: read -u5 dndname

Just another question

How do I redirect the output for the coprocess to a file??
I tried using exec 5>"test.log" new to this stuff.......but does not
work
any sample code would be helpful.
 
 
 

Writing Shell script to interactively control another shell script??

Post by nospamplss » Tue, 16 Jan 2007 23:23:27

Thanks just what I needed,
script.sh |&
exec 4>&p
10: exec 5<&p
11:
12: cat namefile
13:
14: # read the names from a file "namefile"
15: while read uname; do
16: print -u4 $uname
17: read -u5 dndname

Just another question

How do I redirect the output for the coprocess to a file??
I tried using exec 5>"test.log" new to this stuff.......but does not
work
any sample code would be helpful.
 
 
 

Writing Shell script to interactively control another shell script??

Post by Janis Papa » Wed, 17 Jan 2007 02:33:01

XXXX@XXXXX.COM wrote:

[ Please don't top-post! ]


I suppose you mean "_from_ the coprocess" ?


Something like...

bc >file.out |&
print -p '3*5'
# result is now in file.out


Janis

 
 
 

Writing Shell script to interactively control another shell script??

Post by bsh » Thu, 18 Jan 2007 12:16:26


Perusing the code I confess that I'm a little vague about
your intention to distinguish the control script from a simple
menu frontend. Regardless, I see that you may benefit from
one or more function libraries that implement rudimentary IPC
for such things.

Although primarily meant to control daemons, look at:

"ctl.ksh":
http://www.yqcomputer.com/

And for the menu component, why not take advantage of the
many, many menu builders and templates available?

"menubuilder.bash":
http://www.yqcomputer.com/

"frenchmenus.ksh93":
http://www.yqcomputer.com/

"UMENU.pl":
http://www.yqcomputer.com/
"EMDL.pl": "Easy Menu Definition Language"
http://www.yqcomputer.com/

"xenmenu.c":
http://www.yqcomputer.com/
ftp://sunsite.unc.edu/pub/Linux/apps/misc/
"Xenmenu: An ASCII Menu Generator":
http://www.yqcomputer.com/

How I've usually implemented simple IPC is through a
coprocess. If I could modify the code, I would use signal
USR1 to signal the server process to read a line of data
through a named pipe.

=Brian
 
 
 

Writing Shell script to interactively control another shell script??

Post by nospamplss » Sun, 21 Jan 2007 15:51:06

Thanks for all the help. I have completed what i wanted to do. I wasn,t
trying to do the menu instead of typing in my choice for another simple
menu driven sh script(not supposed to change anything in this) manually
I can just run my script. I have redirected the output of the coprocess
to another temp log file that I can see what I have entered, can read
the file using grep to extract what I want.
will look at the source of ctl.ksh just to see how it starts stop and
read other daemons thanks.