2016-11-04 99 views
-2

在linux中編寫shell腳本的新手是一種新手。它是一個csh腳本,但我在bash shell中運行它,爲什麼我使用#!/bin/bash而不是#!/bin/cshwhile循環在shell腳本中不能在linux bash shell中工作

1 #!/bin/bash 
    2 set i = 1 
    3 echo it starts 
    4 
    5 while ($i <= 5) 
    6   echo i is $i 
    7   @ i= $i +1 
    8 end 

**注意:**數字只是代表行數。

上面的代碼給我錯誤的輸出:

it starts 
./me.csh: line 9: syntax error: unexpected end of file 

我無法弄清楚什麼是錯的,即使它回聲it starts並沒有爲錯誤中指定沒有行號9。

+2

你的問題標籤,標題和家當說'bash',但你的腳本'csh'。 'bash'和'csh'是兩種不同的語言。你打算用哪種語言編寫腳本? –

+0

我打算寫'csh',但是我正在從'bash'外殼運行它 –

+0

坦率地說,我會鼓勵你用bash堆棧... – paulsm4

回答

2

的家當必須設置應該解釋殼劇本。你從哪個shell運行腳本並不重要。唯一重要的是劇本寫入的語言。

你的腳本寫在csh,因此必須有shebang #!/bin/csh。即使你想從bash運行它也是如此。此外,你在你的at符號,包換缺少空間:

$ cat me.csh 
#!/bin/csh 
set i = 1 
echo it starts 

while ($i <= 5) 
     echo i is $i 
     @ i = $i + 1 
end 

輸出:

$ ./me.csh 
it starts 
i is 1 
i is 2 
i is 3 
i is 4 
i is 5 
+0

謝謝,但我得到這個錯誤:'bash:./me.csh:/ bin/csh:糟糕的解釋器:沒有這樣的文件或目錄。而我使用Kali linux,我不知道它是否沒有'csh'。 –

+0

如果它沒有csh,則必須安裝csh或將腳本重寫爲您支持的語言。 –

+0

我安裝了'csh',現在它使用'#!/ bin/csh'工作。感謝您的想法。 –

0

試試這個:

#!/bin/bash 
echo it starts 

i=1 
while [ $i -le 5 ]; do 
    echo i is $i 
    i=$((i+1)) 
done 

輸出示例:

it starts 
i is 1 
i is 2 
i is 3 
i is 4 
i is 5 

這裏是一個很好的參考:

BASH Programming - Introduction HOW-TO

+0

很抱歉,但也許我沒有更具體。我編輯了我的問題。 –