2010-09-11 48 views
2

我正在嘗試爲bash腳本mentioned here編寫一個C shell等效腳本。這個C shell腳本有什麼問題?

這是我有:

#! /bin/tcsh 

set now=`date +%Y%m%d%H%M.%S` 
if ((! -f "./cache") || (-n "`find ./monme -newer ./cache`")) 
then 
    touch cache -t "$now" 
    echo "new files added" | mail -s "new build" [email protected] 
endif 

,這是錯誤我得到

$ ./scr 
if: Badly formed number. 
$ 

This page提到,「在C殼號必須是整數」,所以我嘗試

set now=`date +%Y%m%d%H%M` 

但我仍然得到相同的錯誤。

+0

圓括號和短劃線之間'(-n .....))間距* n *和* then *應該和* if *在同一行上? – t0mm13b 2010-09-11 18:32:33

+0

@ tommieb75:仍然是一樣的錯誤。 ((!-f「./cache」)| – Lazer 2010-09-11 18:34:54

+0

@Lazer:最後一擊......'(-n eval(「'find ./monme -newer ./cache \'」)))' – t0mm13b 2010-09-11 18:36:50

回答

3

我你的腳本砍倒在此:

#! /bin/tcsh 

if (-n "`find ./monme -newer ./cache`") then 
    echo hello 
endif 

這給了同樣的錯誤。我認爲罪魁禍首是

-n "`find ./monme -newer ./cache`" 

-n應該做什麼?我認爲它想要一個數字,但得到別的東西...

更新:-n在bash中表示「字符串的長度不爲零」。在我的版本的tcsh它是易於更換爲使用==「」是這樣的:

if ((! -f "./cache") || ("`find ./monme -newer ./cache`" != "")) 
then 
    touch cache -t "$now" 
    echo "new files added" | mail -s "new build" [email protected] 
endif 

試一下,看看它是否工作。

+0

@彼得:我自己以前沒有用過它。從[本頁](http://www.mkssoftware.com/docs/man1/csh.1.asp),「-n 解析命令但不執行它們,這有助於檢查shell腳本的語法」。 – Lazer 2010-09-11 19:48:41

+0

據我瞭解,當你運行它時,-n是csh的一個選項(正如'tcsh -n「echo hello there''),而不是你可以在這樣的情況下使用的東西。 -n雖然在if中似乎意味着什麼,但我無法在我的手冊頁中很快找到它。 – 2010-09-11 19:51:25

+0

好的,我看到你是從bash腳本中逐字逐句的。在bash中,或者測試命令中,-n表示以下字符串的長度應該不爲零。由於這在tcsh中不起作用,所以您必須在tcsh中找到等價物或重構您的代碼。 – 2010-09-11 19:59:22