2012-01-20 56 views
1

我嘗試執行一些類似這樣的不能運行簡單的bash腳本

#!/bin/bash 
${postid=41930} 
while ${postid} < 42000; 
do 
`node title.js ${postid}` 
    ${postid} = ${postid} +1; 
done 

我有這樣的錯誤:

: command not found30 
run.sh: line 8: syntax error: unexpected end of file 

$ echo $SHELL 
/bin/bash 
$ 

從人SH

while *list*;do *list*;done 

sh version 3.2 

回答

2

有在你的腳本幾個地方是固定的:

  1. 正如chepner說你不能將值分配給像${postid}的評估結果,而不是左手使用postid直接你的任務的一面

  2. 你的腳本中應該有一些不可見的字符。嘗試運行dos2unix myscript.sh或嘗試用手鍵入下面的代碼到一個新的文件

https://gist.github.com/1651190

檢查要點
+1

非常感謝您的解決方案。我必須使用vi:set ff = unix。知道所有的工作! –

1

也許,你想

for((postid=41930;postid<42000;++postid)) do 
node title.js $postid 
done 
+0

#!/ bin中/對於bash ((=帖子ID 41930;帖子ID <41940; ++帖子ID))做 節點title.js $帖子ID 完成 $ SH run.sh 「un.sh:第2行:語法錯誤鄰近意外的標記'做 「un.sh:第2行:'用於((=帖子ID 41930;帖子ID <41940; ++帖子ID))做 –

+0

@ v.tsurka,問題被標記'bash',不'sh'。 –

2

另一種快速的方法,只使用bash特性是:

#!/bin/env bash 
for postid in {41930..41999} ; do node title.js ${postid} ; done 

參考文獻:http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion

+0

BC-C8-D8-EB-65:googlecache的MacBookPro $ SH run.sh run.sh:3號線:語法錯誤:文件 –

+1

意外結束,除非有隱藏在你的文件的字符,我不知道是什麼是錯的。你是否手工輸入了代碼? – Karolos

+0

我在vi FS = UNIX和所有的作品)設置 –

1

其他的答案可能是你想用什麼。僅供參考,這裏是你的錯誤來自哪裏。

${postid=41930} 

要分配到41930 posted,只需使用postid=41930。請注意等號周圍沒有空格!

while ${postid} < 42000; 

{} postid是可選的; $postid的作用也一樣。你確實需​​要在命令中包裝這個條件擴展,因爲while循環不能使用裸表達式。像while [ $postid < 42000 ];。請注意,在這種情況下,必須有空格將[]與表達式的其餘部分分開。

do 
    `node title.js ${postid}` 
    ${postid} = ${postid} +1; 

爲了給變量賦值,bash不允許等號周圍有空格。使用空格,它通過擴展$postid來解釋此行,並將其視爲要運行的命令,其中=作爲第一個參數。使用postid=$postid + 1;。在左邊,不需要美元符號,因爲您不擴展變量的值,而是分配一個名稱。在右邊,你需要美元符號的值posted。需要

done