2012-07-12 21 views
0

我在UNIX新手,我試圖寫一個腳本,將用grep在某個字和打印線和文件的地方被發現,如果它的可用和echo不可用,如果它WASN 「T發現用grep在如果statment在UNIX

這裏是我的代碼

#!/bin/csh 
foreach file(`cat file1`) 
set a=`echo $file | grep -n "ok" ` 
if(" $a" -ne 0) then 
echo "$a" | echo $file 
else 
echo "not found" 
endif 
end 
+1

除非你有非常具體的原因,更喜歡'csh',一個共同的建議是堅持一個Bourne-兩者兼容的外殼腳本和交互式使用。 Bash和Zsh都很受歡迎且廣泛可用。 – tripleee 2012-07-12 10:02:01

回答

0

正確的語法是

#!/bin/csh 
foreach file(`cat file1`) 
set a=`echo $file | grep -n "ok" ` 
if(" $a" != " ") then 
echo "$a" 
echo $file 
else 
echo "not found" 
endif 
end