LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 03:32 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Rep: Reputation: 131Reputation: 131
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 03:34 PM. Reason: update error message
 
Old 01-14-2016, 03:34 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

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

Rep: Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408
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, 06:14 PM   #4
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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, 06:15 PM   #5
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Original Poster
Rep: Reputation: 131Reputation: 131
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, 02:38 PM   #6
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Original Poster
Rep: Reputation: 131Reputation: 131
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 02:40 PM.
 
Old 01-15-2016, 03:14 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Code:
if rpm -qa | grep -q gpg-pubkey; then 
echo Installed; 
else 
echo Not Installed
fi
 
Old 01-15-2016, 03:31 PM   #8
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Original Poster
Rep: Reputation: 131Reputation: 131
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, 04:13 PM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:54 PM
how to check rpm package is installed in the fedora 7? melvinong Linux - Server 1 07-19-2007 03:38 AM
How to check SW installed not by rpm maxgg Linux - Newbie 1 12-23-2004 10:29 AM
How to check if a rpm Package is already installed? DarkExile Linux - Newbie 4 12-14-2004 10:41 AM
How to check if such xyz.rpm is already installed? copernicus Linux - Newbie 4 10-16-2003 01:28 PM


All times are GMT -5. The time now is 12:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration