2012-11-23 33 views
0

我正在製作一個bash腳本。其目標是: 執行程序等待幾秒鐘重置程序並重復該過程。 我做2個腳本,但我不知道哪裏是錯誤...而在bash腳本中

#!/bin/bash 
while true; 
do 
seg=`date +%M`; 
if [[ "$seg" -eq "30" ]]; 
then killall sox; 
echo "reset"; 
fi 
done 

慶典:錯誤sintácticoCERCA德爾ELEMENTO inesperado';」

#!/bin/bash 
while true; 
do 
nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default && 
done 

慶典:錯誤sintácticoCERCA德爾ELEMENTO inesperado'做」

+0

第二個腳本的用意是什麼?如果舊的死亡過程開始一個新的'sox'過程?那麼就是失去'&&'。 – tripleee

+0

我們很多人都不知道「sintácticocerca del elemento inesperado」的意思是...不要讓我們猜... – twalberg

回答

1

問題與腳本#1:

;符號是在同一行運行多個命令,一個又一個。 Bash的語法要求在不同的行(同樣與if ...then,並通過;分離,如果在同一行命令語句通常不與在bash一個;字符終止whiledo

從改變你的代碼:。

#!/bin/bash 
while true; 
do 
seg=`date +%M`; 
if [[ "$seg" -eq "30" ]]; 
then killall sox; 
echo "reset"; 
fi 
done 

要:

#!/bin/bash 
while true 
do 
    seg=`date +%M` 
    if [[ "$seg" -eq "30" ]]; then 
     killall sox 
     echo "reset" 
    fi 
done 

Script#2的問題:

&表示將命令作爲後臺進程運行。 &&被用於條件命令鏈接,如:

#!/bin/bash 
while true; 
do 
nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default && 
done 

要:

#!/bin/bash 
while true 
do 
    nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default & 
done 

更改「如果&&前的前一個命令成功,則&&後運行下一個命令」

+0

謝謝。第二個腳本沒有語法錯誤,但無法正常工作。 –

+0

你可能想要'&',而不是'&&' –

+1

其實,你可能不想'&'。這會使系統充斥着背景'sox'進程。 – tripleee