如何绕过外部进程的确认提示?
如何将此 Perl 代码转换为 Groovy?
如何绕过外部进程的确认提示?
我正在尝试将 Perl 脚本转换为 Groovy。该程序正在自动加载/删除maestro(作业调度)作业。问题是删除命令将提示对其找到的每个作业进行确认(Y/N)。我尝试在 groovy 中执行该进程,但会在出现提示时停止。 Perl 脚本正在将一堆 Y 写入流并将其打印到处理程序(如果我理解正确的话)以避免停止。我想知道如何在 Groovy 中做同样的事情?
或者任何其他方法来执行命令并以某种方式在每个确认提示上写 Y。
Perl 脚本:
$maestrostring="";
while ($x < 1500) {
$maestrostring .= "y\n";
$x++;
}
# delete the jobs
open(MAESTRO_CMD, "|ssh mserver /bin/composer delete job=pserver#APPA@")
print MAESTRO_CMD $maestrostring;
close(MAESTRO_CMD);
这是我的工作常规代码:
def deleteMaestroJobs (){
...
def commandSched ="ssh $maestro_server /bin/composer delete sched=$primary_server#$app_acronym$app_level@"
def commandJobs ="ssh $maestro_server /bin/composer delete job=$primary_server#$app_acronym$app_level@"
try {
executeCommand commandJobs
}
catch (Exception ex ){
throw new Exception("Error executing the Maestro Composer [DELETE]")
}
try {
executeCommand commandSched
}
catch (Exception ex ){
throw new Exception("Error executing the Maestro Composer [DELETE]")
}
}
def executeCommand(command){
def process = command.execute()
process.withWriter { writer ->
writer.print('y\n' * 1500)
}
process.consumeProcessOutput(System.out, System.err)
process.waitFor()
}
How can I convert this Perl code to Groovy?
How to bypass confirmation prompts of an external process?
I am trying to convert a Perl script to Groovy. The program is loading/delete maestro (job scheduling) jobs automatically. The problem is the delete command will prompt for confirmation (Y/N) on every single job that it finds. I tried the process execute in groovy but will stop at the prompts. The Perl script is writing bunch of Ys to the stream and print it to the handler( if I understood it correctly) to avoid stopping. I am wondering how to do the same thing in Groovy ?
Or any other approach to execute a command and somehow write Y on every confirmation prompt.
Perl Script:
$maestrostring="";
while ($x < 1500) {
$maestrostring .= "y\n";
$x++;
}
# delete the jobs
open(MAESTRO_CMD, "|ssh mserver /bin/composer delete job=pserver#APPA@")
print MAESTRO_CMD $maestrostring;
close(MAESTRO_CMD);
This is my working groovy code:
def deleteMaestroJobs (){
...
def commandSched ="ssh $maestro_server /bin/composer delete sched=$primary_server#$app_acronym$app_level@"
def commandJobs ="ssh $maestro_server /bin/composer delete job=$primary_server#$app_acronym$app_level@"
try {
executeCommand commandJobs
}
catch (Exception ex ){
throw new Exception("Error executing the Maestro Composer [DELETE]")
}
try {
executeCommand commandSched
}
catch (Exception ex ){
throw new Exception("Error executing the Maestro Composer [DELETE]")
}
}
def executeCommand(command){
def process = command.execute()
process.withWriter { writer ->
writer.print('y\n' * 1500)
}
process.consumeProcessOutput(System.out, System.err)
process.waitFor()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接翻译就是:
A direct translation would be:
另一种选择是使用
yes
:An other option is to use
yes
:标准 Unix 程序 yes 就是为这类事情而设计的。
请注意,如果有机会,
yes
将输出无限量。但当与这样的管道一起使用时,它只是比管道的另一端领先一步,等待它赶上读取速度,最后在操作系统给它一个 SIGPIPE 时退出。The standard Unix program
yes
is made for this sort of thing.Note that
yes
will output an infinite amount given the chance. But when used with a pipe like this, it just stays one step ahead of the other end of the pipe, and waits for it to catch up reading, and finally quits when the OS gives it a SIGPIPE.