LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-14-2016, 02:32 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Bash if then else to check if an rpm is installed not working


So I'm trying to write a hardening script in Bash. A part of it will check if rpm gpg pubkeys are installed.

I'm not sure how to setup the rpm as a variable, if it is installed then to output it to the then statement, and this is what I'm getting when I run it.

Code:
./rpm_test.sh 
Confirm GPG Keys are installed./rpm_test.sh: line 6: [:  rpm -qa gpg-pubkey 2>&1 > /dev/null : 
syntax error in expression (error token is "gpg-pubkey 2>&1 > /dev/null ")

Code:
printf "Confirm GPG Keys are installed" 

if $[ rpm -qa gpg-pubkey 2>&1 > /dev/null ]; then 
 printf "GPG Keys are installed" 
 else "GPG Keys need to be installed" 
fi

Last edited by JockVSJock; 01-14-2016 at 02:34 PM. Reason: update error message
 
Old 01-14-2016, 02:34 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
if [ rpm -qa gpg-pubkey 2>&1 > /dev/null ]; then
perhaps.
 
Old 01-14-2016, 04:56 PM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
if you installed rpm's on rhel using yum
then the hash was checked

and yum will NAG!!!! you to install the keys if you did not
 
Old 01-14-2016, 05:14 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Habitual View Post
Code:
if [ rpm -qa gpg-pubkey 2>&1 > /dev/null ]; then
perhaps.
Remove the square brackets and you should be fine.

Explanation: whatever comes between the if and the semicolon is executed and evaluated. A value of 0 is interpreted as TRUE and the then branch will be entered.
 
1 members found this post helpful.
Old 01-14-2016, 05:15 PM   #5
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by John VV View Post
if you installed rpm's on rhel using yum
then the hash was checked

and yum will NAG!!!! you to install the keys if you did not
Let's say in this case its all RPM, and yum is not an option.
 
Old 01-15-2016, 01:38 PM   #6
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by berndbausch View Post
Remove the square brackets and you should be fine.

Explanation: whatever comes between the if and the semicolon is executed and evaluated. A value of 0 is interpreted as TRUE and the then branch will be entered.
Yes, you are right.

Code:
printf "Confirm if GPG Keys are installed... \n" 
  
if rpm -q gpg-pubkey 2>&1 > /dev/null ; then 
 printf "GPG Keys rpms are installed \n" 
else "GPG Keys need to be installed \n" 
fi
That code works, however some of the other examples that I've seen online refers to bash [[ conditional construct and using $<( command goes here )>

https://digitalvectorz.wordpress.com...ator-expected/

Example

Code:
if [[ rpm -q gpg-pubkey 2>&1 > /dev/null ]]; then 
 printf "GPG Keys rpm are installed \n" 
 else "GPG Keys need to be installed \n"
fi
It blows up with the following:

Code:
./rpm_test2.sh 
Confirm if GPG Keys are installed...
./rpm_test2.sh:  line 6:  conditional binary operator expected 
./rpm_test2.sh:  line 6:  syntax error `-q'
./rpm_test2.sh:  line 6:  `if [[ rpm -q gpg-pubkey 2>&1 > /dev/null ]]; then'
...so let me ask this...how do I set the rpm query output to appear in the then statement? I've added a variable...however I don't know...

Code:
printf "Confirm if GPG Keys are installed... \n" 
  
rpm_version=$

if rpm -q gpg-pubkey 2>&1 > /dev/null ; then 
 printf "GPG Keys rpms are installed $rpm_version \n" 
else "GPG Keys need to be installed" 
fi
Code:
./rpm_test.sh 
Confirm if GPG Keys are installed...
GPG Keys rpm are installed $
If I run this from the CLI

Code:
rpm -q gpg-pubkey 
gpg-pubkey-fd431d51-4ae0493b 
gpg-pubkey-2fa658e0-45700c69

Last edited by JockVSJock; 01-15-2016 at 01:40 PM.
 
Old 01-15-2016, 02:14 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
if rpm -qa | grep -q gpg-pubkey; then 
echo Installed; 
else 
echo Not Installed
fi
 
Old 01-15-2016, 02:31 PM   #8
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by Habitual View Post
Code:
if rpm -qa | grep -q gpg-pubkey; then 
echo Installed; 
else 
echo Not Installed
fi
That's a simple way of doing it so that it works.

However if I have to output details into a report for, say a manager or auditor.

So I'm still having issues with the variable.
 
Old 01-15-2016, 03:13 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
PACKAGE=gpg-pubkey
if rpm -qa | grep -q $PACKAGE; then  echo "$PACKAGE is installed"; else  echo "$PACKAGE needs to be installed"; fi
 
1 members found this post helpful.
  


Reply

Tags
bash, rpm, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
check installed software version using rpm cccc Red Hat 2 11-22-2008 04:54 PM
how to check rpm package is installed in the fedora 7? melvinong Linux - Server 1 07-19-2007 02:38 AM
How to check SW installed not by rpm maxgg Linux - Newbie 1 12-23-2004 09:29 AM
How to check if a rpm Package is already installed? DarkExile Linux - Newbie 4 12-14-2004 09:41 AM
How to check if such xyz.rpm is already installed? copernicus Linux - Newbie 4 10-16-2003 12:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration