2015-11-29 287 views
1
#!/bin/bash 
#sh j 

find . -name "*first*" && echo "file found" || echo "file not found" 

read -p "Run command $foo? [yn]" answer 

case "$answer" in 
y*) find . -type f -exec rename 's/(.*)\/(.*)first(.*)/$1\/beginning_$2changed$3/' {} + ;; 

n*) echo "not renamed" ;; 
esac 
fi 

我想通過文件夾和子文件夾腳本循環,找到包含某些字符串,然後有一個選項,重命名文件或讓它成爲文件(這是Y/N選項)後,腳本應繼續查找。遍歷文件夾中的文件名和子文件夾

我也有,說:「語法錯誤意外的標記‘科幻’」

+1

實際上,你爲什麼最後放了一個'fi'?你的代碼中似乎沒有任何'if'。 – OznOg

+0

如果您希望擴大變量,則需要在重命名錶達式中使用雙引號。此外,請將您的代碼片段粘貼到http://www.shellcheck.net/以跟蹤錯誤 – fedorqui

+0

噢,謝謝,它現在正在工作。但我仍然有問題,代碼重命名後的第一個案件的所有文件。 – Daniel

回答

1

試試這個問題:

#bin/bash 

    handle_file(){ 

      local file=$0 
      local pattern=some_pattern 
      if [[ $(grep -c ${pattern} ${file}) -gt 0 ]]; 
      then 
        ...................................... 
        do anything you want with your ${file} 
        ...................................... 
      fi 
    } 
    export -f handle_file 

    find . -type f -exec bash -c 'handle_file "[email protected]"' {} \; 

handle_file是一個將被調用爲handle_function <filename>一個功能,所以<filename>可用作爲$0裏面的函數。