0

I want to check whether a specific Linux package has been installed or not using th rpm command in a single line. However , it's always giving me a positive result as service present, even if httpd is not installed, can you please point out issue here?

rpm -qa httpd && echo "service present" || echo " service not present"
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I don't think `rpm` has a useful exit status, not with your code at least. – Jetchisel Sep 15 '22 at 17:16
  • 1
    Try running `rpm -qa foo ; echo $?` , if the output is `0` then `&&` is useless in your code. – Jetchisel Sep 15 '22 at 17:18
  • 1
    `package=$(rpm -q httpd); ((${#package})) && echo "service present"` There might be a better way but, there is your one-liner – Jetchisel Sep 15 '22 at 17:22
  • @Jetchisel This is not working as well, always getting service present as a result – Chamara Madhushanka Sep 15 '22 at 17:34
  • Use an `if` clause/statement not that short circuit, that is a cause for electrocution... `if ((${#package})); then foo; else bar; fi` – Jetchisel Sep 15 '22 at 17:39
  • 2
    Sorry, are you using Bash or sh? [They're not the same thing.](/questions/5725296/difference-between-sh-and-bash) Please [edit] and fix the tags. – wjandrea Sep 15 '22 at 17:47

1 Answers1

0

Use the following by substituting the <PACKAGE_NAME>

Shorter

rpm -q <PACKAGE_NAME> && echo "service present" || echo "service not present"

Longer

rpm -qa <PACKAGE_NAME> | grep <PACKAGE_NAME> && echo "service present" || echo "service not present"

Example:

rpm -qa httpd | grep httpd && echo "service present" || echo "service not present"
# Output
[root@bfa3155b8feb /]# rpm -qa httpd | grep httpd && echo "service present" || echo "service not present"
service not present

Vab
  • 412
  • 1
  • 11