簡體   English   中英

在 bash 腳本中自動按下回車鍵

[英]Automate pressing enter key inside of bash script

我有一個 Bash 腳本,我嘗試在其中獲取一個 repo,我必須按 Enter 鍵輸入很長的 Eula 文本,實際上我必須手動按 Enter 鍵 20 次以上,然后鍵入 yes。 有人可以幫我自動按下回車鍵,最后在 bash 腳本中輸入 yes。

yes "" | command

echo -ne '\n' | <command> 

對我不起作用,我仍然必須手動按 Enter 鍵。

這是代碼,所以當它運行 wget 時,我需要一直按 Enter 直到它詢問您是否要接受許可證,然后我輸入“是”。

#!/bin/bash

pid=$(ps aux | grep xxx-server |grep -v grep | awk '{print $2}')
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

我不知道你想做什么,你最好給我們看一些代碼! 但很可能你想使用期望:

#!/usr/bin/expect

spawn "yourscript"

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

盡管這個問題已經很老了。 這個答案可能對某人有所幫助。

您可以使用它來為您的腳本提供輸入。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM