【问题标题】:Calling another shell script with bash and specifying input使用 bash 调用另一个 shell 脚本并指定输入
【发布时间】:2014-02-17 14:09:17
【问题描述】:

我正在尝试使用 bash 调用另一个 shell 脚本,但是该脚本不接受任何命令行参数。相反,它会运行,检查一些事情,然后提示用户输入如下:

Checking....done
Please enter ID #: (user input here)

可以这样做吗?

【问题讨论】:

  • 您在寻找read 命令吗?
  • 反过来。我试图调用的脚本是使用读取命令来获取用户输入并相应地处理它。

标签: bash shell


【解决方案1】:

试试这样的:

echo "My id" | ./script
Checking... done
Please enter ID: My id

./script << EOF
My_id
another input
EOF

Checking... done
Please enter ID: My_id
<blah>
Please enter something: anoter input

【讨论】:

    【解决方案2】:

    解决方法是使用expect

    #!/usr/bin/expect
    spawn php /path/to/script
    expect "ID #: "
    send "{ID#}\r"
    interact
    

    参考:Simulate user input in bash script

    【讨论】:

      【解决方案3】:

      如果你有单个输入来馈送,那么 echo 就足够了,

      回显“输入”| ./script.sh

      如果您有超过 1 个输入,那么 expect 是很好的唯一选择,

      如上所述录制一个脚本然后运行它

      期望 sampleexpect.expec

      【讨论】:

        猜你喜欢
        • 2014-04-06
        • 1970-01-01
        • 2012-01-11
        • 1970-01-01
        • 2012-12-27
        • 1970-01-01
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多