在 bash 脚本中自动按下回车键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18726651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:31:37  来源:igfitidea点击:

Automate pressing enter key inside of bash script

bash

提问by user2548441

I have a Bash script where I try to wget a repo for which I have to press enter for a very long Eula text, literally I have to manually press enter key 20+ times and then type yes. Can someone please help me to automate pressing enter key and at the end enter yes in bash script.

我有一个 Bash 脚本,我尝试在其中获取一个 repo,我必须按 Enter 键输入很长的 Eula 文本,实际上我必须手动按 Enter 键 20 次以上,然后键入 yes。有人可以帮我自动按下回车键,最后在 bash 脚本中输入 yes。

yes "" | command

and

echo -ne '\n' | <command> 

doesn't work for me, I still have to manually press enter key.

对我不起作用,我仍然必须手动按 Enter 键。

Here is the code, so when it runs wget, I need to keep pressing enter till it ask if you want to accept the license and there I enter "yes".

这是代码,所以当它运行 wget 时,我需要一直按 Enter 直到它询问您是否要接受许可证,然后我输入“是”。

#!/bin/bash

pid=$(ps aux | grep xxx-server |grep -v grep | awk '{print }')
if [ -n "$pid" ]; then
    kill $pid
    echo -e  "\n$pid killed -Success"
fi
# Check if the package already installed then remove it

if [ `dpkg -l | grep xxx-server  | wc -l` -gt "0"  ]
then
    apt-get -y remove  xxx-server
fi
# wget the repo using silent install
if [ "apt-cache search xxx-server" ];then
#if [ $? != 0 ] ; then
        echo -e  "\nremoving repo ...."
        #aptitude install $i -y > /dev/null
         apt-get purge xxx-server-repo-* -y  > /dev/null
         echo -e  "\nInstalling Reop .........."

 yes "" | wget -q -O - http://xxx-server.com/ | sh

fi

回答by Aleks-Daniel Jakimenko-A.

I don't know what are you trying to do, you'd better show us some code! But most probably you want to use expect:

我不知道你想做什么,你最好向我们展示一些代码!但很可能你想使用期望:

#!/usr/bin/expect

spawn "yourscript"

for {set i 0} {$i < 20} {incr i 1} {
    send "\n"
}
send "yes\n"

回答by Deva

Even though the question is pretty old. This answer might help someone.

尽管这个问题已经很老了。这个答案可能对某人有所帮助。

you can use this to supply input to your scripts.

您可以使用它为您的脚本提供输入。

cat <(echo "yes") | "Your_Command"