多个条件如下所示,它可以很好地处理理想的文本文件,但是我的代码的实际输出应该有一些额外的行。
同时循环多个条件:
while read -r line && ([[ ! "${line/"[ebuild"}" = "${line}" ]] && [[ -n "${line}" ]])
do
echo "This is the line: $line."
done如果我将代码修改为下面的代码,它就可以正常工作了。
while read -r line
do
if [ ! "${line/"[ebuild"}" = "${line}" ] && [ -n "${line}" ]; then
echo "This is the line: $line."
fi
done理想的文本文件:
[ebuild R ] app-arch/xz-utils-5.2.2::gentoo USE="nls static-libs* threads" 0 KiB
[ebuild R ] sys-libs/zlib-1.2.8-r1::gentoo USE="static-libs -minizip" 0 KiB
[ebuild R ] virtual/libintl-0-r2::gentoo 0 KiB
...实际文本文件:
These are the packages that would be merged, in order:
Calculating dependencies ... done!
[ebuild R ] app-arch/xz-utils-5.2.2::gentoo USE="nls static-libs* threads" 0 KiB
[ebuild R ] sys-libs/zlib-1.2.8-r1::gentoo USE="static-libs -minizip" 0 KiB
[ebuild R ] virtual/libintl-0-r2::gentoo 0 KiB它有什么问题?非常感谢!
发布于 2015-11-16 22:10:45
while循环执行到条件为false为止;然后停止循环并在下面继续。第二个版本执行您想要的操作:在文件结束前循环,但只有在行满足特定条件时才执行主体( echo命令)。
另一方面,第一个版本运行循环,直到文件结束或读取不符合条件的行为止。因为第一行不满足这些条件,所以它会立即退出循环,永远不会到达满足条件的行。
https://stackoverflow.com/questions/33745460
复制相似问题