如何绕过外部进程的确认提示?

发布于 2024-12-17 10:53:21 字数 1460 浏览 1 评论 0原文

如何将此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-12-24 10:53:21

直接翻译就是:

'ssh mserver /bin/composer delete job=pserver#APPA@'.execute().withWriter { writer ->
    writer.print('y\n' * 1500)
}

A direct translation would be:

'ssh mserver /bin/composer delete job=pserver#APPA@'.execute().withWriter { writer ->
    writer.print('y\n' * 1500)
}
清眉祭 2024-12-24 10:53:21

另一种选择是使用 yes

system( qq(ssh mserver "yes y | /bin/composer delete job=pserver#APPA\@") );

An other option is to use yes:

system( qq(ssh mserver "yes y | /bin/composer delete job=pserver#APPA\@") );
梦巷 2024-12-24 10:53:21

标准 Unix 程序 yes 就是为这类事情而设计的。

yes | ssh mserver /bin/composer delete job=pserver#APPA@

请注意,如果有机会,yes 将输出无限量。但当与这样的管道一起使用时,它只是比管道的另一端领先一步,等待它赶上读取速度,最后在操作系统给它一个 SIGPIPE 时退出。

The standard Unix program yes is made for this sort of thing.

yes | ssh mserver /bin/composer delete job=pserver#APPA@

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文