0

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks Dominique for the repy. Actually I was making a custom script to set up a complete dev environment in Vagrant. So I cannot redirect the entire output of the script to NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.