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 and shell scripting languages 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 04:21 PM
how to access root priveliges if root password is lost wojtyla Linux 1 02-18-2005 06:24 AM
Lost root password / Can't login as root Perderabo Answers to Frequently Asked Questions 0 06-22-2004 08:40 PM
No root password cyno UNIX for Dummies Questions & Answers 6 08-15-2002 01:57 AM
root password silver40 Shell Programming and Scripting 9 03-21-2002 04:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-19-2008
dave123 dave123 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 46
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
  #2 (permalink)  
Old 01-19-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
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

  #3 (permalink)  
Old 01-19-2008
dave123 dave123 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 46
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 .
  #4 (permalink)  
Old 01-20-2008
pjottum pjottum is offline
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..
  #5 (permalink)  
Old 01-22-2008
dave123 dave123 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 46
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
  #6 (permalink)  
Old 01-22-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0