2013-05-17 56 views
6

有一個快速的問題,如何回答是在bash腳本

想象,我有這樣的代碼:

mkdir -p $INSTALLDIR 
sudo apt-get install -y git clojure leiningen 
git clone git://github.com/maltoe/storm-install.git 
./storm-install/storm_install.sh all `hostname` $INSTALLDIR 

而且這個腳本會問,你想安裝其他軟件包,我想要說yes, 如何自動執行此操作?

或者可能有一種方法可以在默認情況下回答「是」嗎?

+4

[是](http://linux.die.net/man/1/yes),有。 – hammar

回答

4

命令假設風暴提出問題 - 使用此文件 - 例如:

mkdir -p $INSTALLDIR 
sudo apt-get install -y git clojure leiningen 
git clone git://github.com/maltoe/storm-install.git 
./storm-install/storm_install.sh all `hostname` $INSTALLDIR <<-EOF 
yes 
EOF 

的EOF可以是任何無意義的字符外殼不會解釋。

+0

太棒了!有用! – Vor

5

有了適當命名yes

+1

例如是| command.sh –

3

apt也有--force-yes選項,它可能會有所幫助:

--force-yes 
     Force yes; This is a dangerous option that will cause apt to 
     continue without prompting if it is doing something potentially 
     harmful. It should not be used except in very special situations. 
     Using force-yes can potentially destroy your system! Configuration 
     Item: APT::Get::force-yes. 
+0

謝謝,這是一個不錯的選擇 – Vor

5

我不知道,但我建議嘗試:

echo yes | ./storm-install/storm_install.sh all `hostname` $INSTALLDIR 
+0

真的有用,謝謝 –

4

如果你正在運行一個腳本,試試這個:

yes "yes" | bash script.sh 
2

我有一個腳本需要通過「是」幾次,最後我們需要通過「版本號」才能部署到聯結。 有什麼建議嗎?

0
(echo yes ; echo no; echo yes) | script.sh