How to check if a package is installed from Bash?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
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
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You have to insert blanks after '[' and before ']':if [ $? -eq 0 ]
– muclux
17 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
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 bydpkg -ldo not indicate installed packages. There may be removed packages, too. Note also thatdpkg -llist much more than just package names, so you have to be more careful, if you examine its output bygrep.
– jarno
15 hours ago
add a comment |
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
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
bash awk
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 5 hours ago
v010dya
7022929
7022929
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 17 hours ago
BElluuBElluu
234
234
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
BElluu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You have to insert blanks after '[' and before ']':if [ $? -eq 0 ]
– muclux
17 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
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 bydpkg -ldo not indicate installed packages. There may be removed packages, too. Note also thatdpkg -llist much more than just package names, so you have to be more careful, if you examine its output bygrep.
– jarno
15 hours ago
add a comment |
You have to insert blanks after '[' and before ']':if [ $? -eq 0 ]
– muclux
17 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
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 bydpkg -ldo not indicate installed packages. There may be removed packages, too. Note also thatdpkg -llist much more than just package names, so you have to be more careful, if you examine its output bygrep.
– jarno
15 hours ago
You have to insert blanks after '[' and before ']':
if [ $? -eq 0 ]– muclux
17 hours ago
You have to insert blanks after '[' and before ']':
if [ $? -eq 0 ]– muclux
17 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
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
15 hours ago
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
15 hours ago
add a comment |
2 Answers
2
active
oldest
votes
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
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
16 hours ago
@BElluu, please use "Ask Question" button and create new question. But in general you can replacedpkg -lwithpip listto use as source the list of packages, installed withpip
– Romeo Ninov
15 hours ago
You could examine exit code ofdpkg -l | grep 'ansible', and even use-q` option withgrep, but it is bad habit to examine the output ofdpkg -lthat loosely. For example you would always find something bydpkg -l | grep packed.
– jarno
14 hours ago
add a comment |
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.
When testing exit code ofdpkg, you may want to redirect standard error to null as well like this>/dev/null 2>&1.
– jarno
14 hours ago
Another way to use exit status with pipe is by usingset -o pipefail. Seehelp setfor more information.
– jarno
14 hours ago
add a comment |
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
});
}
});
BElluu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
16 hours ago
@BElluu, please use "Ask Question" button and create new question. But in general you can replacedpkg -lwithpip listto use as source the list of packages, installed withpip
– Romeo Ninov
15 hours ago
You could examine exit code ofdpkg -l | grep 'ansible', and even use-q` option withgrep, but it is bad habit to examine the output ofdpkg -lthat loosely. For example you would always find something bydpkg -l | grep packed.
– jarno
14 hours ago
add a comment |
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
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
16 hours ago
@BElluu, please use "Ask Question" button and create new question. But in general you can replacedpkg -lwithpip listto use as source the list of packages, installed withpip
– Romeo Ninov
15 hours ago
You could examine exit code ofdpkg -l | grep 'ansible', and even use-q` option withgrep, but it is bad habit to examine the output ofdpkg -lthat loosely. For example you would always find something bydpkg -l | grep packed.
– jarno
14 hours ago
add a comment |
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
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
answered 16 hours ago
Romeo NinovRomeo Ninov
49818
49818
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
16 hours ago
@BElluu, please use "Ask Question" button and create new question. But in general you can replacedpkg -lwithpip listto use as source the list of packages, installed withpip
– Romeo Ninov
15 hours ago
You could examine exit code ofdpkg -l | grep 'ansible', and even use-q` option withgrep, but it is bad habit to examine the output ofdpkg -lthat loosely. For example you would always find something bydpkg -l | grep packed.
– jarno
14 hours ago
add a comment |
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
16 hours ago
@BElluu, please use "Ask Question" button and create new question. But in general you can replacedpkg -lwithpip listto use as source the list of packages, installed withpip
– Romeo Ninov
15 hours ago
You could examine exit code ofdpkg -l | grep 'ansible', and even use-q` option withgrep, but it is bad habit to examine the output ofdpkg -lthat loosely. For example you would always find something bydpkg -l | grep packed.
– jarno
14 hours ago
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
16 hours ago
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
16 hours ago
@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
15 hours ago
@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
15 hours ago
You could examine exit code of
dpkg -l | grep 'ansible', and even use -q` option with grep, but it is bad habit to examine the output of dpkg -l that loosely. For example you would always find something by dpkg -l | grep packed.– jarno
14 hours ago
You could examine exit code of
dpkg -l | grep 'ansible', and even use -q` option with grep, but it is bad habit to examine the output of dpkg -l that loosely. For example you would always find something by dpkg -l | grep packed.– jarno
14 hours ago
add a comment |
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.
When testing exit code ofdpkg, you may want to redirect standard error to null as well like this>/dev/null 2>&1.
– jarno
14 hours ago
Another way to use exit status with pipe is by usingset -o pipefail. Seehelp setfor more information.
– jarno
14 hours ago
add a comment |
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.
When testing exit code ofdpkg, you may want to redirect standard error to null as well like this>/dev/null 2>&1.
– jarno
14 hours ago
Another way to use exit status with pipe is by usingset -o pipefail. Seehelp setfor more information.
– jarno
14 hours ago
add a comment |
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.
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.
edited 15 hours ago
answered 15 hours ago
Xen2050Xen2050
6,94622344
6,94622344
When testing exit code ofdpkg, you may want to redirect standard error to null as well like this>/dev/null 2>&1.
– jarno
14 hours ago
Another way to use exit status with pipe is by usingset -o pipefail. Seehelp setfor more information.
– jarno
14 hours ago
add a comment |
When testing exit code ofdpkg, you may want to redirect standard error to null as well like this>/dev/null 2>&1.
– jarno
14 hours ago
Another way to use exit status with pipe is by usingset -o pipefail. Seehelp setfor more information.
– jarno
14 hours ago
When testing exit code of
dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.– jarno
14 hours ago
When testing exit code of
dpkg, you may want to redirect standard error to null as well like this >/dev/null 2>&1.– jarno
14 hours ago
Another way to use exit status with pipe is by using
set -o pipefail. See help set for more information.– jarno
14 hours ago
Another way to use exit status with pipe is by using
set -o pipefail. See help set for more information.– jarno
14 hours ago
add a comment |
BElluu is a new contributor. Be nice, and check out our Code of Conduct.
BElluu is a new contributor. Be nice, and check out our Code of Conduct.
BElluu is a new contributor. Be nice, and check out our Code of Conduct.
BElluu is a new contributor. Be nice, and check out our Code of Conduct.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You have to insert blanks after '[' and before ']':
if [ $? -eq 0 ]– muclux
17 hours ago
Even after your edit you are still missing those spaces aroung [ ].
– muclux
16 hours ago
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 -ldo not indicate installed packages. There may be removed packages, too. Note also thatdpkg -llist much more than just package names, so you have to be more careful, if you examine its output bygrep.– jarno
15 hours ago