LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-23-2010, 04:16 PM   #1
thespaceman
LQ Newbie
 
Registered: Oct 2008
Posts: 16

Rep: Reputation: 0
Best way to code rpm pre/post scripts


I'm having a few problems coding a post install script for my custom RPM package. I'm putting the script directly in the %post section of the spec files. For example if I wanted to add a user after the package is installed I would add the following code. The problem is capturing the output of the commands which should be stored in the variable.

%post
#Add some user (bob)

myuser=`grep ^bob: /etc/passwd`

# If myuser is empty then we have to add bob
if [ -z "$myuser" ]; then
useradd -u 123 -g 10 ..... -s /bin/sh bob
fi


The problem seems to be that myuser is always null even if bob exists in /etc/passwd.

What's wrong with this code and should I use an external script instead?
TIA
 
Old 03-23-2010, 07:29 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You don't need to store in a variable as you can use the exit code directly. First turn to common utilities like 'id' and such, else use something like 'awk' or 'grep', so why not simply
Code:
id apache >/dev/null|| adduser --lotsa-args
? Of course the user name has to be unique for such an example to work.
 
Old 03-24-2010, 12:04 AM   #3
thespaceman
LQ Newbie
 
Registered: Oct 2008
Posts: 16

Original Poster
Rep: Reputation: 0
OK thanks for the reply. As this package only needs a user to be created this is quite straightforward and I can use the return codes from id etc as you have suggested.

Just so that I can understand this better what is the problem with trying to store values returned from commands in variables within RPM scripts? Is this possible or was RPM designed with this 'feature' and if so can we only use return codes within test expressions?

Are there any good examples available of pre/post scripts that could shed some light on this?
 
Old 03-24-2010, 03:38 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
There's no problem storing variables as RPM uses "plain" shell scripts. This works:
Code:
%post
if [ $1 -eq 1 ]; then
	# This package is being installed for the first time
	echo "Post: installing %{name}"
	myuser=$(grep '^bob:' /etc/passwd 2>/dev/null)
	if [ ${#myuser} -eq 0 ]; then
		echo "Post: useradd ... bob"
	fi
else
	# This is an upgrade
fi
If you have any problems with it please add debug statements to your %post scripts that echo variables and contents and post the output.
 
  


Reply



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
Call scripts in Rpm's: %pre %post section vibhor_agarwalin Linux - General 4 04-13-2009 12:18 AM
problem related to pre and post increment vijaya_svk Programming 10 07-05-2008 05:19 PM
Post/pre Routing shipon_97 Linux - Networking 1 02-25-2006 01:26 AM
Question about rpm spec files and %post scripts rpg Linux - Software 5 01-06-2006 07:20 AM
FEA solver and pre-post processor tungsten Linux - Software 1 07-14-2003 06:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:40 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