Visit Our UNIX and Linux User Community


ask for root password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ask for root password
# 1  
Old 01-19-2008
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  
Old 01-19-2008
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  
Old 01-19-2008
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  
Old 01-20-2008
Quote:
Originally Posted by dave123
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  
Old 01-22-2008
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  
Old 01-22-2008
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.
# 7  
Old 01-22-2008
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #663
Difficulty: Medium
American computer programmer Timothy Paterson wrote the original operating system for the Intel 8086 microprocessor in 1980, initially calling it QDOS (Quick and Dirty Operating System).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can a root role change the root password in Solaris 10?

i do not have root on a solairs 10 server , however i do have the root role, i was wondering if I can change the root password as a a role with the passwd command? I have not tried yet. and do i have to use the # chgkey -p afterwards? i need to patch is why i am asking. thanks (1 Reply)
Discussion started by: goya
1 Replies

2. Ubuntu

Root access that can't change root password?

We are having a little problem on a server. We want that some users should be able to do e.g. sudo and become root, but with the restriction that the user can't change root password. That is, a guarantee that we still can login to that server and become root no matter of what the other users will... (2 Replies)
Discussion started by: 244an
2 Replies

3. Solaris

Lost Root Password on VXVM Encapsulated Root Disk

Hi All Hope it's okay to post on this sub-forum, couldn't find a better place I've got a 480R running solaris 8 with veritas volume manager managing all filesystems, including an encapsulated root disk (I believe the root disk is encapsulated as one of the root mirror disks has an entry under... (1 Reply)
Discussion started by: sunnyd76
1 Replies

4. Solaris

Solaris 8 - Asks for current root password when trying to change root password.

Hello All, I have several solaris boxes running Solaris 8. When changing root passwords on them, all will simply ask for the new root password to change and of course to re-type the new password. One of the systems however asks for the existing root password before it will display the new password... (8 Replies)
Discussion started by: tferrazz
8 Replies

5. Shell Programming and Scripting

how to change root password using shell script with standard password

Hi Friends. I am new to scripting now i want to change the root password using the script with standard password. which is the easy scripting to learn for the beginner, Thanks in advance. (2 Replies)
Discussion started by: kurva
2 Replies

6. HP-UX

Root Password

Hi all, I am changing the root password periodically from eg: password01 to password02. However, I can still use password01 after it is changed, it seems that it only authenticates up the first few characters. Please can someone let me know how I can fix this. Thanks Ryan (1 Reply)
Discussion started by: macgre_r
1 Replies

7. Linux

root password

Hi everybody: I have a problem. How does it possible that when I type the root password on terminal, later write: su password: ****** System recognize the password, but when a I use the GUI environment, the system does not recognize it. Any suggestion. :o Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

8. UNIX for Dummies Questions & Answers

I need it to prompt me for a root password, so I don't have to log as root

Hi folks, I'm trying to install a program, and I want to place some of the executables into /usr/bin so that they can be executed from any folder on the computer. I've been giveng the root password, but told never to log in directly as root. Instead, I can wait for a password prompt. However, I... (2 Replies)
Discussion started by: lunchtime
2 Replies

9. Linux

how to access root priveliges if root password is lost

wish to know how to access root password it root password is forgotten in linux (1 Reply)
Discussion started by: wojtyla
1 Replies

10. Answers to Frequently Asked Questions

Lost root password / Can't login as root

We have quite a few threads about this subject. I have collected some of them and arranged them by the OS which is primarily discussed in the thread. That is because the exact procedure depends on the OS involved. What's more, since you often need to interact with the boot process, the... (0 Replies)
Discussion started by: Perderabo
0 Replies

Featured Tech Videos