Azeem
Azeem

Reputation: 145

How to restrict output in shell script of a command executing in loop

I'm having a shell script as follows

#!/usr/bin/env bash


#Packages list

declare -a packages=( git build_essential node )
declare -a packages_status

# installing=`apt-get install -y `
installing="echo "

for i in "${packages[@]}"
do
    packages_status[$i]=$(dpkg-query -W -f='${status}' $i | grep "install ok installed")
    # echo ${packages_status[$i]}
done

The line of code

packages_status[$i]=$(dpkg-query -W -f='${status}' $i | grep "install ok installed")

produces the following output

dpkg-query: no packages found matching build_essential

dpkg-query: no packages found matching node

I want that LOC to execute without producing any output.

Upvotes: 0

Views: 399

Answers (2)

MarcoS
MarcoS

Reputation: 17721

dpkg-query command ouputs errors to stderr, not stdout.
So, you should link the two channels before piping to grep:

packages_status[$i]=$(dpkg-query -W -f='${status}' $i 2>&1 | grep "install ok installed")

This way the script will only print lines "install ok installed" for installed packages.

Upvotes: 2

Dominique
Dominique

Reputation: 17551

In order not to see error output, you can redirect that output (stream number 2) to the NULL device:

Do_Whatever 2>/dev/null

In order not to see any output, you can redirect normal output (stream number 1) to the NULL device and redirect error output there too:

Do_Whatever >/dev/null 2>&1

Upvotes: 1

Related Questions