2013-03-21 41 views
0

我的每一個文件遍歷一個目錄,並試圖查找/替換用下面這段代碼的文件的路徑部分...查找/上的文件名稱替換使用的sed

for f in /the/path/to/the/files/* 
do 
    file = $(echo $f | sec 's/\/the\/path\/to\/the\/files\///g`); 
done 

然而,我的代碼賦值部分出現以下錯誤...

cannot open `=' (No such file or directory) 

我在做什麼錯了?

回答

3

您必須編寫=無空格周圍:

for f in /the/path/to/the/files/* 
do 
file=$(echo $f | sec 's/\/the\/path\/to\/the\/files\///g'); 
done 

此外,倒不如用另一種符號,而不是/,作爲sed的分隔符:

for f in /the/path/to/the/files/* 
do 
file=$(echo $f | sec '[email protected]/the/path/to/the/files/@@g') 
done 
1

你不能把空格在等號的兩邊:

for f in /the/path/to/the/files/* 
do 
    file=$(echo $f | sed 's/\/the\/path\/to\/the\/files\///g`); 
done 

參數擴展是一個更好的w然而AY做到這一點,:

for f in /the/path/to/the/files/* 
do 
    file=${f#/the/path/to/the/files/} 
done 
0

嘗試:

for f in /the/path/to/the/files/*; do 
    # no spaces around = sign 
    file=$(echo $f | sed "s'/the/path/to/the/files/''g"); 
done