How to check if a package is installed from Bash?Replace string in bash scriptHow do I teach bash in Ubuntu some curse words?Using awk or similar to find the next word in the output of another commandPrint out the amount of times 2 words appear in the syslog. But also have it tell me how many times for each hourPrint a string a number of timesHow do I escape apostrophes within an awk statement?When placing the results of a awk operation into a variable, my results are being overwrittenbash - return and print statementFind an executable file in a directory and assign it to a variable through bashHow can I store the output of an awk command as a bash script variable?

Average spam confidence

Print the string equivalents of a phone number

How to build suspense or so to establish and justify xenophobia of characters in the eyes of the reader?

Are "living" organ banks practical?

What are the peak hours for public transportation in Paris?

When conversion from Integer to Single may lose precision

Frame failure sudden death?

Does a 3rd-level Wolf Totem barbarian get advantage against enemies when an ally is within 5 feet of the enemy?

Does an ice chest packed full of frozen food need ice?

Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut

Movie about a boy who was born old and grew young

Genetic limitations to learn certain instruments

Why only the fundamental frequency component is said to give useful power?

What can plausibly explain many of my very long and low-tech bridges?

How to officially communicate to a non-responsive colleague?

How does an ordinary object become radioactive?

What's the name of this light airplane?

Is there a word for a man who behaves like a woman?

Why doesn’t a normal window produce an apparent rainbow?

Do simulator games use a realistic trajectory to get into orbit?

Can the poison from Kingsmen be concocted?

PTFE detoriation temperature

When did Linux kernel become libre software?

Should I give professor gift at the beginning of my PhD?



How to check if a package is installed from Bash?


Replace string in bash scriptHow do I teach bash in Ubuntu some curse words?Using awk or similar to find the next word in the output of another commandPrint out the amount of times 2 words appear in the syslog. But also have it tell me how many times for each hourPrint a string a number of timesHow do I escape apostrophes within an awk statement?When placing the results of a awk operation into a variable, my results are being overwrittenbash - return and print statementFind an executable file in a directory and assign it to a variable through bashHow can I store the output of an awk command as a bash script variable?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I need to check if a specific package is installed on a machine from within a Bash script.



I found something like that but I don't know how use it correctly.



dpkg -l | grep "ansible" | awk 'print $2'
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


I need check, if command dpkg -l | grep "ansible" | awk 'print $2' return me word "Ansible" then echo OK, else echo FAIL.



@EDIT



I think better will be command



dpkg -l | grep "ansible" | awk 'print $2'


so this command return me two words:



ansible
ansible_lint


How should I use this bash script? If I doing something like that:



ansible = $?
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


that's not work, but I'm sure I doing that wrong. How can I read result from dpkg -l | grep "ansible" | awk 'print $2' command and if I get word "ansible" script will print OK, if not print FAIL.










share|improve this question
























  • You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

    – muclux
    Apr 14 at 8:12











  • Even after your edit you are still missing those spaces aroung [ ].

    – muclux
    Apr 14 at 8:50











  • What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

    – jarno
    Apr 14 at 10:24

















1















I need to check if a specific package is installed on a machine from within a Bash script.



I found something like that but I don't know how use it correctly.



dpkg -l | grep "ansible" | awk 'print $2'
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


I need check, if command dpkg -l | grep "ansible" | awk 'print $2' return me word "Ansible" then echo OK, else echo FAIL.



@EDIT



I think better will be command



dpkg -l | grep "ansible" | awk 'print $2'


so this command return me two words:



ansible
ansible_lint


How should I use this bash script? If I doing something like that:



ansible = $?
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


that's not work, but I'm sure I doing that wrong. How can I read result from dpkg -l | grep "ansible" | awk 'print $2' command and if I get word "ansible" script will print OK, if not print FAIL.










share|improve this question
























  • You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

    – muclux
    Apr 14 at 8:12











  • Even after your edit you are still missing those spaces aroung [ ].

    – muclux
    Apr 14 at 8:50











  • What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

    – jarno
    Apr 14 at 10:24













1












1








1








I need to check if a specific package is installed on a machine from within a Bash script.



I found something like that but I don't know how use it correctly.



dpkg -l | grep "ansible" | awk 'print $2'
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


I need check, if command dpkg -l | grep "ansible" | awk 'print $2' return me word "Ansible" then echo OK, else echo FAIL.



@EDIT



I think better will be command



dpkg -l | grep "ansible" | awk 'print $2'


so this command return me two words:



ansible
ansible_lint


How should I use this bash script? If I doing something like that:



ansible = $?
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


that's not work, but I'm sure I doing that wrong. How can I read result from dpkg -l | grep "ansible" | awk 'print $2' command and if I get word "ansible" script will print OK, if not print FAIL.










share|improve this question
















I need to check if a specific package is installed on a machine from within a Bash script.



I found something like that but I don't know how use it correctly.



dpkg -l | grep "ansible" | awk 'print $2'
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


I need check, if command dpkg -l | grep "ansible" | awk 'print $2' return me word "Ansible" then echo OK, else echo FAIL.



@EDIT



I think better will be command



dpkg -l | grep "ansible" | awk 'print $2'


so this command return me two words:



ansible
ansible_lint


How should I use this bash script? If I doing something like that:



ansible = $?
if [$? -eq 0]; then
echo OK
else
echo FAIL
fi


that's not work, but I'm sure I doing that wrong. How can I read result from dpkg -l | grep "ansible" | awk 'print $2' command and if I get word "ansible" script will print OK, if not print FAIL.







bash awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 14 at 20:14









v010dya

7342930




7342930










asked Apr 14 at 7:58









BElluuBElluu

234




234












  • You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

    – muclux
    Apr 14 at 8:12











  • Even after your edit you are still missing those spaces aroung [ ].

    – muclux
    Apr 14 at 8:50











  • What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

    – jarno
    Apr 14 at 10:24

















  • You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

    – muclux
    Apr 14 at 8:12











  • Even after your edit you are still missing those spaces aroung [ ].

    – muclux
    Apr 14 at 8:50











  • What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

    – jarno
    Apr 14 at 10:24
















You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

– muclux
Apr 14 at 8:12





You have to insert blanks after '[' and before ']': if [ $? -eq 0 ]

– muclux
Apr 14 at 8:12













Even after your edit you are still missing those spaces aroung [ ].

– muclux
Apr 14 at 8:50





Even after your edit you are still missing those spaces aroung [ ].

– muclux
Apr 14 at 8:50













What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

– jarno
Apr 14 at 10:24





What do you want to do by that code? Do you want to check if a package whose name contains 'ansible' is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep.

– jarno
Apr 14 at 10:24










3 Answers
3






active

oldest

votes


















3














You can check if the software is installed on this way:



if [ "$(dpkg -l | awk '/ansible/ print '|wc -l)" -ge 1 ]; then
echo OK
else
echo FAIL
fi


You can't use exit code because it will be from awk and in this case always be 0






share|improve this answer























  • Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    – BElluu
    Apr 14 at 9:12











  • @BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

    – Romeo Ninov
    Apr 14 at 9:35











  • This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

    – jarno
    Apr 20 at 16:41


















1














If you know the exact package name, you can just ask dpkg if it's installed with



dpkg -l packagename


For example:



$ dpkg -l pulsea
dpkg-query: no packages found matching pulsea


The exit code is also 1 (fail) if a package isn't installed, you can test for that (as seen later).



$ dpkg -l pulseaudio
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server


Here the exit code is 0 (success), so you can do this too



$ if dpkg -l pulseaudio; then echo yes;fi
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
yes


Note the trailing "yes" above. But now since you can just use the exit code, you don't really care about dpkg's output, so ignore it with an if or an && (AND list):



$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi
yes

$ dpkg -l pulseaudio >/dev/null && echo yes
yes


dpkg can also match partial names, using asterisks, like



$ dpkg -l "*pulse*"



About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you'll have to do something like examining the $PIPESTATUS[@]:



$ false | true
$ echo $PIPESTATUS[@]
1 0


And like $?, $PIPESTATUS[@] changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example



ansible = $?
if [$? -eq 0]; then


$? has already changed by the if test, and it's probably 0 since assigning a variable like that almost always succeeds.






share|improve this answer

























  • When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

    – jarno
    Apr 14 at 10:34






  • 1





    Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

    – jarno
    Apr 14 at 10:39











  • @jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

    – Xen2050
    Apr 15 at 6:18


















0














To check, if package named 'pkgname' is successfully installed, use



if dpkg-query -W -f'$db:Status-Abbrevn' pkgname 2>/dev/null 
| grep -q '^.i $'; then
echo 'Installed'
else
echo 'Not installed'
fi


See man dpkg-query about usage of dpkg-query. With -q option grep does not output anything, but exits with code 0 if and only if there is a match.






share|improve this answer























  • FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

    – Xen2050
    Apr 15 at 6:08












  • @Xen2050 and it does not print the header lines.

    – jarno
    Apr 15 at 14:32











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1133749%2fhow-to-check-if-a-package-is-installed-from-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can check if the software is installed on this way:



if [ "$(dpkg -l | awk '/ansible/ print '|wc -l)" -ge 1 ]; then
echo OK
else
echo FAIL
fi


You can't use exit code because it will be from awk and in this case always be 0






share|improve this answer























  • Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    – BElluu
    Apr 14 at 9:12











  • @BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

    – Romeo Ninov
    Apr 14 at 9:35











  • This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

    – jarno
    Apr 20 at 16:41















3














You can check if the software is installed on this way:



if [ "$(dpkg -l | awk '/ansible/ print '|wc -l)" -ge 1 ]; then
echo OK
else
echo FAIL
fi


You can't use exit code because it will be from awk and in this case always be 0






share|improve this answer























  • Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    – BElluu
    Apr 14 at 9:12











  • @BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

    – Romeo Ninov
    Apr 14 at 9:35











  • This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

    – jarno
    Apr 20 at 16:41













3












3








3







You can check if the software is installed on this way:



if [ "$(dpkg -l | awk '/ansible/ print '|wc -l)" -ge 1 ]; then
echo OK
else
echo FAIL
fi


You can't use exit code because it will be from awk and in this case always be 0






share|improve this answer













You can check if the software is installed on this way:



if [ "$(dpkg -l | awk '/ansible/ print '|wc -l)" -ge 1 ]; then
echo OK
else
echo FAIL
fi


You can't use exit code because it will be from awk and in this case always be 0







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 14 at 8:48









Romeo NinovRomeo Ninov

597310




597310












  • Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    – BElluu
    Apr 14 at 9:12











  • @BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

    – Romeo Ninov
    Apr 14 at 9:35











  • This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

    – jarno
    Apr 20 at 16:41

















  • Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    – BElluu
    Apr 14 at 9:12











  • @BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

    – Romeo Ninov
    Apr 14 at 9:35











  • This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

    – jarno
    Apr 20 at 16:41
















Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

– BElluu
Apr 14 at 9:12





Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

– BElluu
Apr 14 at 9:12













@BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

– Romeo Ninov
Apr 14 at 9:35





@BElluu, please use "Ask Question" button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip

– Romeo Ninov
Apr 14 at 9:35













This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

– jarno
Apr 20 at 16:41





This is bad practice in several ways: there could be several packages whose name contain string "ansible", but it could be that none of them is named "ansible". The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep 'ansible' that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.

– jarno
Apr 20 at 16:41













1














If you know the exact package name, you can just ask dpkg if it's installed with



dpkg -l packagename


For example:



$ dpkg -l pulsea
dpkg-query: no packages found matching pulsea


The exit code is also 1 (fail) if a package isn't installed, you can test for that (as seen later).



$ dpkg -l pulseaudio
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server


Here the exit code is 0 (success), so you can do this too



$ if dpkg -l pulseaudio; then echo yes;fi
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
yes


Note the trailing "yes" above. But now since you can just use the exit code, you don't really care about dpkg's output, so ignore it with an if or an && (AND list):



$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi
yes

$ dpkg -l pulseaudio >/dev/null && echo yes
yes


dpkg can also match partial names, using asterisks, like



$ dpkg -l "*pulse*"



About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you'll have to do something like examining the $PIPESTATUS[@]:



$ false | true
$ echo $PIPESTATUS[@]
1 0


And like $?, $PIPESTATUS[@] changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example



ansible = $?
if [$? -eq 0]; then


$? has already changed by the if test, and it's probably 0 since assigning a variable like that almost always succeeds.






share|improve this answer

























  • When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

    – jarno
    Apr 14 at 10:34






  • 1





    Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

    – jarno
    Apr 14 at 10:39











  • @jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

    – Xen2050
    Apr 15 at 6:18















1














If you know the exact package name, you can just ask dpkg if it's installed with



dpkg -l packagename


For example:



$ dpkg -l pulsea
dpkg-query: no packages found matching pulsea


The exit code is also 1 (fail) if a package isn't installed, you can test for that (as seen later).



$ dpkg -l pulseaudio
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server


Here the exit code is 0 (success), so you can do this too



$ if dpkg -l pulseaudio; then echo yes;fi
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
yes


Note the trailing "yes" above. But now since you can just use the exit code, you don't really care about dpkg's output, so ignore it with an if or an && (AND list):



$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi
yes

$ dpkg -l pulseaudio >/dev/null && echo yes
yes


dpkg can also match partial names, using asterisks, like



$ dpkg -l "*pulse*"



About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you'll have to do something like examining the $PIPESTATUS[@]:



$ false | true
$ echo $PIPESTATUS[@]
1 0


And like $?, $PIPESTATUS[@] changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example



ansible = $?
if [$? -eq 0]; then


$? has already changed by the if test, and it's probably 0 since assigning a variable like that almost always succeeds.






share|improve this answer

























  • When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

    – jarno
    Apr 14 at 10:34






  • 1





    Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

    – jarno
    Apr 14 at 10:39











  • @jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

    – Xen2050
    Apr 15 at 6:18













1












1








1







If you know the exact package name, you can just ask dpkg if it's installed with



dpkg -l packagename


For example:



$ dpkg -l pulsea
dpkg-query: no packages found matching pulsea


The exit code is also 1 (fail) if a package isn't installed, you can test for that (as seen later).



$ dpkg -l pulseaudio
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server


Here the exit code is 0 (success), so you can do this too



$ if dpkg -l pulseaudio; then echo yes;fi
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
yes


Note the trailing "yes" above. But now since you can just use the exit code, you don't really care about dpkg's output, so ignore it with an if or an && (AND list):



$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi
yes

$ dpkg -l pulseaudio >/dev/null && echo yes
yes


dpkg can also match partial names, using asterisks, like



$ dpkg -l "*pulse*"



About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you'll have to do something like examining the $PIPESTATUS[@]:



$ false | true
$ echo $PIPESTATUS[@]
1 0


And like $?, $PIPESTATUS[@] changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example



ansible = $?
if [$? -eq 0]; then


$? has already changed by the if test, and it's probably 0 since assigning a variable like that almost always succeeds.






share|improve this answer















If you know the exact package name, you can just ask dpkg if it's installed with



dpkg -l packagename


For example:



$ dpkg -l pulsea
dpkg-query: no packages found matching pulsea


The exit code is also 1 (fail) if a package isn't installed, you can test for that (as seen later).



$ dpkg -l pulseaudio
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server


Here the exit code is 0 (success), so you can do this too



$ if dpkg -l pulseaudio; then echo yes;fi
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================-==============-==============-=========================
ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
yes


Note the trailing "yes" above. But now since you can just use the exit code, you don't really care about dpkg's output, so ignore it with an if or an && (AND list):



$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi
yes

$ dpkg -l pulseaudio >/dev/null && echo yes
yes


dpkg can also match partial names, using asterisks, like



$ dpkg -l "*pulse*"



About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you'll have to do something like examining the $PIPESTATUS[@]:



$ false | true
$ echo $PIPESTATUS[@]
1 0


And like $?, $PIPESTATUS[@] changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example



ansible = $?
if [$? -eq 0]; then


$? has already changed by the if test, and it's probably 0 since assigning a variable like that almost always succeeds.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 14 at 9:58

























answered Apr 14 at 9:51









Xen2050Xen2050

6,97432344




6,97432344












  • When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

    – jarno
    Apr 14 at 10:34






  • 1





    Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

    – jarno
    Apr 14 at 10:39











  • @jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

    – Xen2050
    Apr 15 at 6:18

















  • When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

    – jarno
    Apr 14 at 10:34






  • 1





    Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

    – jarno
    Apr 14 at 10:39











  • @jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

    – Xen2050
    Apr 15 at 6:18
















When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

– jarno
Apr 14 at 10:34





When testing exit code of dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.

– jarno
Apr 14 at 10:34




1




1





Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

– jarno
Apr 14 at 10:39





Another way to use exit status with pipe is by using set -o pipefail. See help set for more information.

– jarno
Apr 14 at 10:39













@jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

– Xen2050
Apr 15 at 6:18





@jarno True, that's a good tip for pipes if something in there fails. I'm pretty sure examining $PIPESTATUS[@] is the only way to figure out what part failed & succeeded though, if that even matters

– Xen2050
Apr 15 at 6:18











0














To check, if package named 'pkgname' is successfully installed, use



if dpkg-query -W -f'$db:Status-Abbrevn' pkgname 2>/dev/null 
| grep -q '^.i $'; then
echo 'Installed'
else
echo 'Not installed'
fi


See man dpkg-query about usage of dpkg-query. With -q option grep does not output anything, but exits with code 0 if and only if there is a match.






share|improve this answer























  • FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

    – Xen2050
    Apr 15 at 6:08












  • @Xen2050 and it does not print the header lines.

    – jarno
    Apr 15 at 14:32















0














To check, if package named 'pkgname' is successfully installed, use



if dpkg-query -W -f'$db:Status-Abbrevn' pkgname 2>/dev/null 
| grep -q '^.i $'; then
echo 'Installed'
else
echo 'Not installed'
fi


See man dpkg-query about usage of dpkg-query. With -q option grep does not output anything, but exits with code 0 if and only if there is a match.






share|improve this answer























  • FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

    – Xen2050
    Apr 15 at 6:08












  • @Xen2050 and it does not print the header lines.

    – jarno
    Apr 15 at 14:32













0












0








0







To check, if package named 'pkgname' is successfully installed, use



if dpkg-query -W -f'$db:Status-Abbrevn' pkgname 2>/dev/null 
| grep -q '^.i $'; then
echo 'Installed'
else
echo 'Not installed'
fi


See man dpkg-query about usage of dpkg-query. With -q option grep does not output anything, but exits with code 0 if and only if there is a match.






share|improve this answer













To check, if package named 'pkgname' is successfully installed, use



if dpkg-query -W -f'$db:Status-Abbrevn' pkgname 2>/dev/null 
| grep -q '^.i $'; then
echo 'Installed'
else
echo 'Not installed'
fi


See man dpkg-query about usage of dpkg-query. With -q option grep does not output anything, but exits with code 0 if and only if there is a match.







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 15 at 5:12









jarnojarno

2,06732151




2,06732151












  • FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

    – Xen2050
    Apr 15 at 6:08












  • @Xen2050 and it does not print the header lines.

    – jarno
    Apr 15 at 14:32

















  • FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

    – Xen2050
    Apr 15 at 6:08












  • @Xen2050 and it does not print the header lines.

    – jarno
    Apr 15 at 14:32
















FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

– Xen2050
Apr 15 at 6:08






FYI, dpkg -l / dpkg --list looks like it runs dpkg-query --list, and dpkg-query -W is "Just like the --list option this will list all packages matching the given pattern" but with a customizable format

– Xen2050
Apr 15 at 6:08














@Xen2050 and it does not print the header lines.

– jarno
Apr 15 at 14:32





@Xen2050 and it does not print the header lines.

– jarno
Apr 15 at 14:32

















draft saved

draft discarded
















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1133749%2fhow-to-check-if-a-package-is-installed-from-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?