Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 读取值中包含空格字符的变量_Bash_Unix_Scripting_Escaping_Whitespace - Fatal编程技术网

Bash 读取值中包含空格字符的变量

Bash 读取值中包含空格字符的变量,bash,unix,scripting,escaping,whitespace,Bash,Unix,Scripting,Escaping,Whitespace,我在变量中存储了一些路径(变量名->测试) 结果: /abc/pqr/filepath1 /abc/pqr/lmn/file path2 /abc/pqr/rst/filepath3 我想转义第二条路径中的“空格”字符,并获取每条路径的所有者,我使用以下命令: stat -c '%U' ${test} , works for 1st and 3rd path. 我如何使第二条道路的工作?提前感谢。使用while构造获取每个文件名,每行一个: while IFS= read -r f; d

我在变量中存储了一些路径(变量名->测试)

结果:

/abc/pqr/filepath1
/abc/pqr/lmn/file path2
/abc/pqr/rst/filepath3
我想转义第二条路径中的“空格”字符,并获取每条路径的所有者,我使用以下命令:

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

我如何使第二条道路的工作?提前感谢。

使用
while
构造获取每个文件名,每行一个:

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

而IFS=read-rf;do stat-c'%U'$f';在一个变量中是否有一个字符串中的所有路径?这似乎是一个更好的使用阵列的地方。如何在
$test
中获取这些路径?但是名称是
文件路径2
,而不是
文件路径2
@BenjaminW。但我还不清楚,如果是这样的话,那就更容易了,不需要移除空间。已编辑。很明显,OP的文件名中包含空格(他希望转义它们,而不是删除它们,以便对
stat
的调用可以工作)。很抱歉造成混淆,我需要在第二个路径中保留空格字符,并且您的while语句可以工作。然而,我想要的结果是这样的:IFS=read-rf;做回显“$(stat-c'%U'$f)”;使用
echo
和命令替换@Macopare可以得到相同的结果。无论如何,如果您坚持,只要在IFS=read-rf时引用变量
f
;做回显“$(stat-c'%U''$f”)”;完成
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