【问题标题】:checking condition then auto answer questions检查条件然后自动回答问题
【发布时间】:2014-03-16 14:09:57
【问题描述】:

我有一个关于 bash 的问题:

这是一种检查条件然后自动回答诸如heredoc 中发生的问题的方法吗?

我知道我们可以自动回答问题的两种方式(通过passwd 命令举例):

1) 管道

<password> | passwd <username>

2)heredocs

passwd <username> <<EOF
<password>
<password>
EOF

现在,问题

如何设置条件来回答他们///

if [ `passwd` -eq 0 ]; then
    <<EOF
     <password>
     <password>
   EOF
fi

例如,这是不可能的,有什么想法吗?

#!/bin/bash                                                                                                                                                                      

mdadm mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1
if [ $? -eq 0 ]; then
    <<EOF                                                                                                                                                                        
    ok                                                                                                                                                                           
EOF                                                                                                                                                                              
elif [ $? -ne 0]; then
    <<EOF                                                                                                                                                                        
    failed                                                                                                                                                                       
EOF                                                                                                                                                                              
fi

欢迎heredocspiping 或任何其他解决方案,

我想先检查条件,然后才能回答问题

谢谢

【问题讨论】:

  • 您问“如何设置条件...”。您不能运行命令并获取其退出状态($?),然后向该命令发送进一步的输入,因为根据定义,它的退出状态在退出之前不可用 - 此时发送它为时已晚任何额外的东西。
  • 这不是我问的......这就是我想要的:如果它是真的(它问一个问题)然后检查条件,然后自动回答它......
  • 如果您自动回答程序提出的问题,程序必须仍在运行才能读取您的回复 - 在这种情况下,它的退出状态将不可用。

标签: linux bash awk pipe heredoc


【解决方案1】:

也许用 expect 命令?

http://www.thegeekstuff.com/2010/12/5-expect-script-command-line-argument-examples/

这是一个 FTP 的小例子,因为我不熟悉 mdadm。您可以看到它根据来自服务器的响应执行不同的操作 - 我认为,这就是您尝试执行的操作。

  # Connect to the FTP server using the "spawn" command.
  spawn ftp $hostname

  # Wait for a login prompt.
  expect -re "(Name|login|Login|Username).*:.*" {

      # Login prompt received. Send username to server.
      exp_send "$username\r"
      exp_send_user "sent username\n"
  } eof {

      # No login prompt received. Display an error.
      exp_send_user "could not connect\n"
  }

【讨论】:

  • 谢谢你,你能不能更好地回答它而不是一个链接
【解决方案2】:

我假设您想检查您的操作的结果。您可以分两步完成:

passwd <user> <<EOF
     <password>
     <password>
EOF

if [ $? -eq 0 ]; then
    echo "Great success"
fi

$? 保存最后执行语句的返回码。

【讨论】:

  • 查看我的更新...我知道如何检查此类结果,我说的是自动回答问题
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2023-03-28
相关资源
最近更新 更多