2016-09-19 47 views
1

我有一些路徑保存在一個變量(變量名 - >測試)讀變量

echo "${test}" 

結果:

/abc/pqr/filepath1 
/abc/pqr/lmn/file path2 
/abc/pqr/rst/filepath3 

我想逃避的「空間」字符在第二個路徑,並獲得每個路徑的所有者,我使用以下命令:

stat -c '%U' ${test} , works for 1st and 3rd path. 

如何使這項工作爲2 nd路徑?提前致謝。

+1

你必須在一個變量一個字符串的所有路徑?這似乎是它可能是一個更好的地方使用數組 –

+1

你如何獲得'$測試'這些路徑? – anubhava

回答

1

使用while結構得到每個文件名,每行一個:

while IFS= read -r f; do stat -c '%U' "$f"; done <<<"$test" 

例子:

$ echo "$test" 
/abc/pqr/filepath1 
/abc/pqr/lmn/file path2 
/abc/pqr/rst/filepath3 

$ while IFS= read -r f; do echo "$f"; done <<<"$test" 
/abc/pqr/filepath1 
/abc/pqr/lmn/file path2 
/abc/pqr/rst/filepath3 
+2

但名稱是'file path2',而不是'filepath2'。 –

+0

@BenjaminW。但我不清楚,如果那樣的話,那麼它會更容易,不需要去除空間。編輯。 – heemayl

+1

很明顯,OP的文件名中包含空格(他想逃避它們,而不是刪除它們,以便調用'stat')。 – chepner