2017-04-27 36 views
0

我採購軟件的shell腳本,但我不斷收到以下錯誤語句(?):猛砸正確的,如果未正確執行

>source thefile.sh 
'else' builtin not inside of if block 

我檢查了理論上的錯誤塊,我不能發現什麼不對的結構

if [ condition ] 
then 
if [ condition2 ] 
then 
dostuff 
else 
dostuffagain 
fi 
else # this is the else which generates the error 
stuffstuffstuff 
fi 

我看不出什麼錯:這是一個嵌套的if/then和每一個如果由它自己的網絡連接終止。爲什麼外殼會對此感到不安?

+2

是否'thefile.sh'直接執行時沒有錯誤執行?這可能是一個有價值的信息,因爲例如當採購 – Aaron

+0

替換'condition'和'stuff'有意義時,shebangs會被忽略,您的示例適用於我。 – Robin479

+0

檢查http://shellcheck.net上的整個文件。祝你好運。 – shellter

回答

1

這不是bash錯誤消息;我發現它在source code for the fish shell

case parse_keyword_else: { 
       this->parse_error(token, parse_error_unbalancing_else, 
            L"'else' builtin not inside of if block"); 

這告訴我你想source一個bash腳本在fish殼,這是行不通的。 source通知shell(在這種情況下爲fish)自己執行腳本,但fish shell的語法與腳本中使用的bash語法太不同了,因爲它是合理的。

(我想你可以嘗試寫一個polyglot script那將會在兩個bashfish工作,但是這將是困難的。)

+0

你是對的,我沒有意識到我還在一個魚殼裏面!謝謝 – Mutewinter