0

I have a unimrcp client process (ucp) and I want to spawn multiple ucp using shell script.

Whenever I spawn a ucp, it returns a command line using which I can access different functionalities in ucp. So I want to write a script that achieves following,

  1. Spawn a ucp process
  2. wait until the ucp's command line appears
  3. access ucp's functionality with a command "run recog"

Note that ./ucp && run recog does not work since ./ucp is finished only after we exit from the command line of ucp.

I tried the following for this,

#!/bin/bash                                                                     
./ucp &                                                               
fg                                                                              
echo "run recog uni2"                                                           
wait                                                                            

However, ./ucp & results into starting the client process and then writing "?????" on the client console resulting into "????" command not found. Following are the logs,

./script.sh

2018-10-23 14:36:53:618851 [INFO]   Set Property ip:127.0.0.1
2018-10-23 14:36:53:618851 [INFO]   Register Codec [PCMU]
2018-10-23 14:36:53:618851 [INFO]   Register Codec [PCMA]
2018-10-23 14:36:53:618851 [INFO]   Register Codec [L16]
2018-10-23 14:36:53:618851 [NOTICE] Register Resource [speechsynth]
2018-10-23 14:36:53:618851 [NOTICE] Register Resource [speechrecog]
2018-10-23 14:36:53:620588 [NOTICE] Register Resource [recorder]
2018-10-23 14:36:53:620588 [NOTICE] Register Resource [speakverify]
2018-10-23 14:36:53:620593 [INFO]   Register Resource Factory
2018-10-23 14:36:53:620600 [NOTICE] Create SofiaSIP Agent [SIP-Agent-1] [1.12.11-227-g73efc97] sip:127.0.0.1:8062;transport=udp
2018-10-23 14:36:53:620600 [INFO]   Register Signaling Agent [SIP-Agent-1]
2018-10-23 14:36:53:620600 [NOTICE] Create RTSP Client [RTSP-Agent-1] [100]
2018-10-23 14:36:53:620600 [INFO]   Register Signaling Agent [RTSP-Agent-1]
2018-10-23 14:36:53:620600 [NOTICE] Create MRCPv2 Agent [MRCPv2-Agent-1] [100]
2018-10-23 14:36:53:620600 [INFO]   Register Connection Agent [MRCPv2-Agent-1]
2018-10-23 14:36:53:620600 [NOTICE] Create Media Engine [Media-Engine-1]
unknown command: ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Is this the right way to achieve what I mentioned in the question? (spawn a client and run a command on its console)? If yes, why does the "./ucp &" results in reading "?????" considering that as input to its console? Note that this does not happen if I simply fire "./ucp & fg " from the console. It can see the expected behavior of the command when I do it from the console. The issue appears when I try to execute the script.

If this is not a right way, How can I achieve what I mentioned in the question? (1, 2, and 3)

Update-- ./ucp & returns the control to a process (background process I think), which consideres whatever outputs printed on the terminal as inputs to the console of ./ucp. I think I need to somehow transfer the IO control to ./ucp process or not? Not sure..

Thank you

  • 2
    Something along the lines of echo "run recog uni2" | ./ucp & possible instead ? – steve Oct 24 '18 at 21:59
  • Thank you Steve. That results into the same issue I mentioned. Specifically, ./ucp & is spawning the client and outputs some of the logs. Whatever logs that it outputs, a process (not sure which one but most probably background process) considers all those logs as the commands that should be entered on the console. I think I need to tell the background process to return the IO control to ./ucp &. what do you think? – sbhatt Oct 24 '18 at 22:05
1

The complicated way is to use expect which fakes a full terminal for the process to play around in:

#!/usr/bin/env expect
# Spawn a ucp process
spawn -noecho ucp
# wait until the ucp's command line appears
expect -ex "whatever the ucp command line prompt looks like here"
# access ucp's functionality with a command "run recog"
send -- "run recog\r"
# not sure what needs to happen here, so let's wait for
# ucp to exit (may also need to handle timeout or 
# the output buffer being full or ...)
expect eof

Then you could run the above script as runucp & or whatever you decide to call it in your shell.

As a side note, the simpler echo blah | ... standard input method will sometimes fail as programs can discard input, for example a program could discard all input prior to presenting the first prompt via either reading and throwing away that input:

% cat prompter
#!/usr/bin/env expect
sleep 3         ;# simulate load of something big and bloated
read stdin      ;# toss any existing input
puts -nonewline "prompt> "
puts "got >>>[gets stdin]<<<"
% print -l a b c d e f g | ./prompter
prompt> got >>><<<

or via a tcflush(3) call. expect allows one to avoid this condition via the expect -x "detect prompt here" call that indicates that the spawned process is likely up and ready for input.

  • You're awesome! That works!! I am a beginner in shell script. I looked into expect but didn't think of the solution in this way. It's pretty cool. Thank you very much! – sbhatt Oct 24 '18 at 23:22
0

Does ucp accept input on stdin? If yes, try piping your command:

echo "run recog" | ./ucp
  • Thank you Rudi. It results into the same issue I mentioned above. I think ./ucp is spawning the client and whatever happens on the terminal after that (logs and all things printed on terminal) are considered as a new command to be executed. I think I need to somehow control the IO and hand it over to a different process. what do you think? – sbhatt Oct 24 '18 at 22:08
  • 1
    I'd be very surprised if output to the screen could be used as an input anywhere else (unless measures are taken like piping or teeing). – RudiC Oct 24 '18 at 22:44
  • Yea. I think of the same. that's why I am not sure what's happing. Thank you very much for looking into it though. I really appreciate this. @thring's solution works. – sbhatt Oct 24 '18 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.