SCIENTIFIC-LINUX-USERS Archives

April 2014

SCIENTIFIC-LINUX-USERS@LISTSERV.FNAL.GOV

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Keith Chadwick <[log in to unmask]>
Reply To:
Keith Chadwick <[log in to unmask]>
Date:
Tue, 29 Apr 2014 14:27:46 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (48 lines)
The way I have done this in the past (using ksu rather than su) is to:

#!/bin/bash
#
if [ "$UID" -ne 0 ]
then
	portion of script to run as root
else
	ksu $0 $*
	if [ "$?" -ne 0 ]
	then
		echo "ksu failed!"
		exit 1
	else
		portion of script to run as non-root
	fi
fi
#
exit

-Keith.
On Apr 29, 2014, at 2:20 PM, ToddAndMargo <[log in to unmask]> wrote:

> Hi All,
> 
> I have a bash script that need to be run as root.
> In the script, I check to see if it is running as
> root and flag the user to run appropriately.
> 
> Is there a way to use "su" to prompt for the password
> and continue the script if successful? (I would test for
> $? after the prompt.)
> 
> Currently "su" will just open a new shell as root.
> 
> I can run a command inside "su", but what about the
> other 200 lines of code?  :'(
> 
> Many thanks,
> -T
> 
> 
> -- 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ATOM RSS1 RSS2