2017-04-02 137 views
7

我有以下bash腳本重複發現每個圖像。它需要重複遍歷所有文件,並替換該文件中所有圖像的所有匹配項。Sed給出:sed:無法讀取:沒有這樣的文件或目錄

for image in app/www/images-theme-dark/*.png 
    do 
     echo "installing icon" $image 

     # extract filename from iconpath 
     iconfile=$(basename $image) 
     iconPath="images/"$(basename $image) 

     # replace paths in all files containing icon paths 
     find app/www -type f \(-name "*.html" -or -name "*.css" -or -name "*.js" \ 
          -or -name "*.appcache" \) \ 
      -exec sed -i '' -e 's|$iconPath|images-theme-dark/$iconfile|g' "{}" \; 

    done 

然而,當我運行該腳本sed給出:

sed: can't read : No such file or directory

在計算器上我發現sed: can't read : No such file or directory但我已經有引號{}

當我呼應sed命令並在命令行手動執行它沒有錯誤。

我使用Raspbian的GNU/Linux 8.0(傑西)GNU的sed v4.2.2

是否有人看到這可能是錯在這裏?

+1

btw:請參閱:[bash中單引號和雙引號之間的區別](http://stackoverflow.com/q/6697753/3776858) – Cyrus

+2

'sed -i'之後是什麼''''? – melpomene

+0

@melpomene它阻止sed創建備份文件並直接替換文件 –

回答

6

編譯自評答案,訣竅是墨爾波墨涅和AlexP。

''sed -i是什麼?

-i表示就地,即直接在文件中編輯。
-i ''表示編輯一個名稱爲空字符串的文件。
由於可能沒有名稱爲空字符串的文件,因此sed會抱怨它無法讀取它。

注1只平臺的依賴
-i的語法是GNU之間的一個區別sed和從Mac OS的sed。

注2 「通常」的順序的參數
-e開關,以指示關於sed代碼允許它具有在文件名之間。
這是一個陷阱(在這個陷阱中,例如,我不好意思地抓到了這個陷阱),讓你在sed命令行中找到你所期望的東西。
它允許
sed -i filename -e "expression" AnotherFileName
這是
sed -i'NoExtensionGiven' "expression" filename AnotherFileName非故意僞裝版本。

相關問題