Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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/2/linux/23.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
Arrays Bash循环2个变量中带有空格的数组_Arrays_Linux_Bash_For Loop - Fatal编程技术网

Arrays Bash循环2个变量中带有空格的数组

Arrays Bash循环2个变量中带有空格的数组,arrays,linux,bash,for-loop,Arrays,Linux,Bash,For Loop,我正在编写一个脚本来查找服务器上目录的磁盘使用情况。 我需要处理名称中带有空格的文件夹 我在使用for循环遍历每个带有空格的变量时遇到问题,因为它会将它们视为两个独立的变量 当前代码段: 这将输出以下内容: anaconda-ks.cfg file test fileusage install.log install.log.syslog 文件“file test”在我的循环中运行后将被拆分 因此,如果我可以在每个变量周围加上引号,那么我就可以继续了。或者,如果有人对我想要达到的目标有更好的想

我正在编写一个脚本来查找服务器上目录的磁盘使用情况。 我需要处理名称中带有空格的文件夹

我在使用for循环遍历每个带有空格的变量时遇到问题,因为它会将它们视为两个独立的变量

当前代码段: 这将输出以下内容:

anaconda-ks.cfg
file test
fileusage
install.log
install.log.syslog
文件“file test”在我的循环中运行后将被拆分

因此,如果我可以在每个变量周围加上引号,那么我就可以继续了。或者,如果有人对我想要达到的目标有更好的想法,我会洗耳恭听

多谢各位

编辑2 循环包含2个数组和存储


正如您所看到的,“文件测试”被视为两个变量。

假设您的数组已正确填充,您的循环应该是:

for d in "${dir[@]}"; do
    # do something with "$d", e.g.
    echo "$d"
done
在循环顶部的数组扩展周围使用双引号,并记住在使用循环变量时引用它

但是,在您的示例中,
$dir
似乎不是数组

我建议将您的代码更改为:

while read -r junk dir rest; do
    d=${dir:6}
    # use "$d" here
done < /root/fileusage/diskusage
在read-r垃圾目录rest时;做
d=${dir:6}
#在这里使用“$d”
完成

这将读取文件的每一行,并将其拆分为三个变量。第二个字段是
$dir
,它包含第二个字段,该字段应与
cut-f2
的输出相等
${dir:6}
从变量中删除前6个字符。

我建议使用
du-0
选项以null终止
du
的输出,并在
循环时使用此
进行迭代:

while read -d '' -r size name; do
    printf "%s -> %s\n" "$size" "$name"
done < <(du -sh -0 /root/*)
读取-d'-r大小名称时;做
printf“%s->%s\n”“$size”“$name”

done<注意此代码中有一些错误。粘贴它来清理它们(提示:
diskusage=du-sh…
是错误的,您可能还想说
diskusage=$(du-sh$scandtest…
),以循环一个列表,其中包含您最好使用的空间
${var[@]}
,如中所示。在您的情况下,
的结果是…
。还请注意,无需将数据存储在文件中,因为输出到变量就足够了。
file test
被视为两个变量,因为shell无法区分分隔文件的空格和命令替换输出中作为文件名一部分的空格。按照Tom Fenech的建议,在使用
的同时使用
阅读
。感谢您的快速回复。我的for循环包含2个数组。我是这样做的:
for((i=0;我正在继续我的测试,一旦计算出来,将更新所有内容。当我运行此操作时,它将在空格后切掉所有内容。此外,我很难使用两个数组将其放入一个循环中。感谢您的帮助。您需要编辑您的问题,以向我们展示我们可以用来重现您的问题的最小示例。目前,不清楚您的出发点是什么。我已将问题更新为我的最新脚本。感谢您的持续帮助。这是使用
gnu du
测试和使用的代码。您的
du
版本是什么?使用
du--version
du的命令输出--
du(gnu coreutils)进行测试8.4
是的,我也有。du-sh-0/root/*
是否显示任何输出?啊哈!我已经制作了一个单独的脚本,它正在工作!还没有完全完成,但我想我可以用它创建我需要的。再次感谢您的帮助。
for d in "${dir[@]}"; do
    # do something with "$d", e.g.
    echo "$d"
done
while read -r junk dir rest; do
    d=${dir:6}
    # use "$d" here
done < /root/fileusage/diskusage
while read -d '' -r size name; do
    printf "%s -> %s\n" "$size" "$name"
done < <(du -sh -0 /root/*)