The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
I need it to prompt me for a root password, so I don't have to log as root lunchtime UNIX for Dummies Questions & Answers 2 06-25-2007 12:21 PM
how to access root priveliges if root password is lost wojtyla Linux 1 02-18-2005 03:24 AM
Lost root password / Can't login as root Perderabo Answers to Frequently Asked Questions 0 06-22-2004 04:40 PM
No root password cyno UNIX for Dummies Questions & Answers 6 08-14-2002 09:57 PM
root password silver40 Shell Programming and Scripting 9 03-21-2002 01:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-19-2008
Registered User
 

Join Date: Dec 2007
Posts: 45
ask for root password

Hey all me again, what do i need to put in my script to check for root privalges and if not then ask for them, what ive got so far is, thankyou in advance for any help.

if [ $(whoami) = "root" ]
then
?????????????????????
then
Reply With Quote
Forum Sponsor
  #2  
Old 01-19-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Here is what I would do...
Code:
if [ $(whoami) != "root" ] ; then
      echo sorry, you must be root to run this script >&2
      exit 1
fi
Reply With Quote
  #3  
Old 01-19-2008
Registered User
 

Join Date: Dec 2007
Posts: 45
Yes thats what i do more or less, but what i want is if not root then to ask for root password (enter root pass) then carry on with the script .
Reply With Quote
  #4  
Old 01-20-2008
Registered User
 

Join Date: Feb 2006
Location: .nl
Posts: 16
Quote:
Originally Posted by dave123 View Post
Yes thats what i do more or less, but what i want is if not root then to ask for root password (enter root pass) then carry on with the script .
Eh

if [ $(whoami) != "root" ] ; then
command
else
su root -c command
fi

the su will ask for a password.

it would be a better idea to use sudo:
if [ $(whoami) != "root" ] ; then
/path/to/command
else
sudo /path/to/command
fi

sudo will ask for the password..
Reply With Quote
  #5  
Old 01-22-2008
Registered User
 

Join Date: Dec 2007
Posts: 45
Thank you for you reply, but i just cant get it to work, sudo is out of the question as
there is no sudo set up
if i run below as root it prints hello, but if i run as non root it will ask for password then, nothing, what am i doing wrong plse,

#!/bin/bash


if [ $(whoami) = "root" ]
then
echo "hello"
else
su root -c echo "hello"
fi
Reply With Quote
  #6  
Old 01-22-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
su root -c "echo hello"
might work. The su command takes the single argument after the -c as the command to be executed. In many cases this winds up being the name of a secondary shell script. In your command you are running echo and your "hello" is just ignored.
Reply With Quote
  #7  
Old 01-22-2008
Registered User
 

Join Date: Dec 2007
Posts: 45
Hi thanks for your reply, it doesnt work im afraid, the problem is i dont want it to run another script, i just want it to carry on with the same script,
below is a example of the script , what i would like is for the script to ask for root password if not in root and then carry on once the root password has been entered, im sure there must be a way.

Quote:
#!/bin/bash

typeset -r YUM="`which yum`"
typeset -r RPM="`which rpm`"

if [ $(whoami) != "root" ] ; then
echo sorry, you must be root to run this script >&2
exit 1
fi

$YUM -y install libXp compat-libstdc++-33 compat-libstdc++-296 wget
wget http://www.frostwire.com/download/?os=redhat
$RPM -Uvh --nodeps frostwire*.rpm
rm -f frostwire*.rpm
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0