[arch-projects] [dbscripts] [PATCH 4/8] Use `grep &>/dev/null` instead of `grep -q` when operating on piped stdin.

Luke Shumaker lukeshu at lukeshu.com
Wed Mar 14 01:52:01 UTC 2018

From: Luke Shumaker <lukeshu at parabola.nu>

`grep -q` may exit as soon as it finds a match; this is a good optimization
for when the input is a file.  However, if the input is the output of
another program, then that other program will receive SIGPIPE, and further
writes will fail.  When this happens, it might (bsdtar does) print a
message about a "write error" to stderr.  Which is going to confuse and
alarm the user.

In one of the cases, this had already been mitigated by wrapping
bsdtar in "echo "$(bsdtar ...)", as Bash builtin echo doesn't complain
if it gets SIGPIPE.  However, that means we're storing the entire
output of bsdtar in memory, which is silly.
 db-functions         | 2 +-
 test/lib/common.bash | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/db-functions b/db-functions
index 58b753a..ee390ff 100644
--- a/db-functions
+++ b/db-functions
@@ -303,7 +303,7 @@ check_pkgfile() {
 	in_array "${pkgarch}" "${ARCHES[@]}" 'any' || return 1
-	if echo "${pkgfile##*/}" | grep -q "^${pkgname}-${pkgver}-${pkgarch}"; then
+	if echo "${pkgfile##*/}" | grep "^${pkgname}-${pkgver}-${pkgarch}" &>/dev/null; then
 		return 0
 		return 1
diff --git a/test/lib/common.bash b/test/lib/common.bash
index 45e4800..ab805dd 100644
--- a/test/lib/common.bash
+++ b/test/lib/common.bash
@@ -215,7 +215,7 @@ checkPackageDB() {
 				for db in ${DBEXT} ${FILESEXT}; do
 					[ -r "${FTP_BASE}/${repo}/os/${repoarch}/${repo}${db%.tar.*}" ]
-					bsdtar -xf "${FTP_BASE}/${repo}/os/${repoarch}/${repo}${db%.tar.*}" -O | grep -q "${pkgfile%${PKGEXT}}"
+					bsdtar -xf "${FTP_BASE}/${repo}/os/${repoarch}/${repo}${db%.tar.*}" -O | grep "${pkgfile%${PKGEXT}}" &>/dev/null
@@ -269,7 +269,7 @@ checkRemovedPackageDB() {
 		for tarch in ${tarches[@]}; do
 			if [ -r "${FTP_BASE}/${repo}/os/${tarch}/${repo}${db%.tar.*}" ]; then
 				for pkgname in ${pkgnames[@]}; do
-					echo "$(bsdtar -xf "${FTP_BASE}/${repo}/os/${tarch}/${repo}${db%.tar.*}" -O)" | grep -qv ${pkgname}
+					bsdtar -xf "${FTP_BASE}/${repo}/os/${tarch}/${repo}${db%.tar.*}" -O | grep -v ${pkgname} &>/dev/null

More information about the arch-projects mailing list