bash 中的 while 循环多个条件有什么问题
What's wrong with this while loop multiple condition in bash
如下所示的多个条件,它在理想的文本文件中工作正常,但我的代码的实际输出应该有一些额外的行。
while 循环多个条件:
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
有什么问题吗?非常感谢!
一个while
循环执行直到条件为假;然后它停止循环并在下面继续。第二个版本做你想做的事:循环直到文件末尾,但只在行满足特定条件时才执行正文(echo
命令)。
另一方面,第一个版本运行循环直到文件末尾或它读取不满足条件的行。由于第一行不满足这些条件,它会立即退出循环并且永远不会到达满足条件的行。
如下所示的多个条件,它在理想的文本文件中工作正常,但我的代码的实际输出应该有一些额外的行。
while 循环多个条件:
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
有什么问题吗?非常感谢!
一个while
循环执行直到条件为假;然后它停止循环并在下面继续。第二个版本做你想做的事:循环直到文件末尾,但只在行满足特定条件时才执行正文(echo
命令)。
另一方面,第一个版本运行循环直到文件末尾或它读取不满足条件的行。由于第一行不满足这些条件,它会立即退出循环并且永远不会到达满足条件的行。