How to handle user prompt/user intervention during shell script execution

帅比萌擦擦* 提交于 2020-03-05 06:08:12

问题


Consider shell scripts which ask user's input to proceed or not[yY/nN] and another script calls all these scripts.
I don't want to give Yy input manually.
Note: User input is not an argument to a script and it occurs anytime during script execution.
Consider 3 shell scripts viz: a.sh, b.sh, c.sh.
On execution, scripts prompt:

"You want to Proceed[Y/y/N/n]?"

Now, Wrapper.sh calls a.sh, b.sh, c.sh and also provides Y/y or N/n input as and when those scripts prompts.
I can not pass an argument as a.sh "Y"
[ps: a.sh b.sh c.sh are not executable and non-editable]
Can not install any third party plugins on RHEL, So need to use only default plugins/commands.


回答1:


You can try expect tool which is used to feed input automatically to an interactive program. Try to begin with something like this:

#!/usr/bin/expect

set timeout 10

spawn "PATH_TO/a.sh"
expect "You want to Proceed" 
send "y\r"

interact



回答2:


I haven't used this myself, but take a look at 'expect'. This exactly what it's there for. Good luck!



来源:https://stackoverflow.com/questions/19954540/how-to-handle-user-prompt-user-intervention-during-shell-script-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!