2013-05-31 60 views
0

這是我的腳本,它正在搜索所有日誌文件並在刪除較舊的壓縮文件時將其壓縮。 但是,當我運行此腳本時出現以下錯誤:運行.sh文件時出現未知的操作員錯誤

./file.sh:test:unknown operator。

代碼:

#Directory of archives 

archive_dr="/u01/apps/weblogic/weblogic10/user_projects/archive" 

#Directory of log files 

logdir="/u01/apps/weblogic/weblogic10/user_projects/domains/BPM/servers/BPM_MS1/logs" 


cd $archive_dr 

#Removing older archived files 

if [ find . \(-name '*.log0*.gz' -o \ 
     -name '*.out0*.gz' \) ] 
then 
    rm *.out00*.gz *.log00*.gz 
fi 

cd $logdir 

#Search,zip and move the new archive files 

if [ find . \(-name '*.log0*' -o -name '*.out0*' \) \ 
      -atime +30 ] 
then 
    for log_files in `find . \(\ 
      -name '*.log0*' -o -name '*.out0*' \ 
     \) -atime +30` 

    do 
     gzip $log_files 
     mv $log_files.gz /u01/a*/w*/w*/us*/archive 
    done 

    if [$? = 0]; then 
     echo "Logs Archieved Successfully"| 
     mailx -s " Logs Archieved Successfully" \ 
      -c '[email protected]' [email protected]' 
    fi 

請建議我要去的地方錯了嗎?

回答

3

變化:

if [ find . \(-name '*.log0*.gz' -o -name '*.out0*.gz' \) ]; then 

到:

if [ "$(find . \(-name '*.log0*.gz' -o -name '*.out0*.gz' \))" ]; then 

你想運行find命令,並測試它是否返回任何輸出。 test命令(這是[是什麼的縮寫)不會執行其內容,它期望它是一個表達式來測試,如if [ "$foo" = 3 ]

另請注意,find遞歸到子目錄中,但您只在當前目錄中有rm。如果您不想遞歸,請添加-maxdepth 1選項。

不需要第二個if。如果find沒有找到任何文件,則for循環將無法操作,並且會立即終止。

+1

除了以上,你需要用'['並在此行'$'之間的空間:'如果[$? = 0];然後',並在'0'之後和'''之前的另一個空間 - 像這樣'if [$? = 0]; then'。 – twalberg

+0

謝謝,但它似乎並沒有在這裏工作。 – coolmego

+0

我故意刪除.gz文件,現在當我運行腳本時,它給了我.gz文件沒有找到 – coolmego

0

對不起,無法正確編輯帖子。但是,得到它運行,:)

CODE

#Directory of archives  
archive_dr="/u01/apps/weblogic/weblogic10/user_projects/archive"  

    #Directory of log files 

logdir="/u01/apps/weblogic/weblogic10/user_projects/domains" 
    cd $archive_dr 
#Removing older archived files 
    find . \(-name '*.log00*.gz' -o -name '*.out00*.gz' \) -exec rm {} \; 

cd $logdir 

    #Search,zip and move the new archive files 

for log_files in `find . \(-name '*.log0*' -o -name '*.out0*' \) -ctime +5` 
    do 

      gzip $log_files 

      mv $log_files.gz /u01/a*/w*/w*/us*/archive 

    done   
    if [ $? = 0 ]; then 

    echo "text"|mailx -s "test" -c [email protected]' [email protected]' 
fi