Bash script prompt for sudo password?

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Bash script prompt for sudo password?
# 1  
Old 04-27-2009
Bash script prompt for sudo password?

I'm making a script that will be a double clickable .command file and I need it to prompt for the users admin password.

So far I have:

Code:
if [[ "$(/usr/bin/whoami)" != "root" ]]; then 
    sudo -p "Please enter your admin password: " date 2>/dev/null 1>&2
        if [ ! $? = 0 ]; then 
            echo "You entered an invalid password or you are not an admin/sudoer user. Script aborted."
            exit 1
        fi
fi

which does get the prompt, but the rest of the script does not run with sudo privileges as needed. What do I need to do for this to apply to the whole script?

Thanks.
# 2  
Old 04-30-2009
the way your script is written is to only run the date comand with sudo. if you want to run other commands you should probably create a second script and call that instead of date.
# 3  
Old 05-01-2009
Try this code:

Code:
sudo -p "Please enter you admin password: " whoami 1>/dev/null && {sudo whoami;sudo whoami;whoami;sudo -k}

Let sudo do the work instead of doubling the effort. Note if the user fails to type their correct admin password or is not an admin user nothing else will run. Each command run as root must be preference with sudo. sudo -k will remove the timestamp so no other sudo commands will run without authentication.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Sudo command prompt for a password

in the /etc/sudoer file this line was added: wtolentino ALL=(ORACLE) NOPASSWD: /bin/chmod when i tried to run this command sudo -u oracle /bin/chmod 775 /appshared/applications/lpa/executables/chrpt001.rep it prompts me for a password for example: $ pwd /appshared/applications/lpa... (2 Replies)
Discussion started by: wtolentino
2 Replies

2. Shell Programming and Scripting

How to add password prompt between script ?

Hi Team, I need password prompt between this script .i want to need put password manually. Instead of adding password in script . Script pause till input password and resume again. #!/usr/bin/expect set ip spawn telnet $ip expect "login:" send "USR\r" expect "*assword*"... (3 Replies)
Discussion started by: Ganesh Mankar
3 Replies

3. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

4. Red Hat

Sudo Password Prompt over SSH

I am not sure what I am missing here. I have the following identical entry in /etc/sudoers on multiple Red Hat 6.4 servers. icinga ALL=NOPASSWD:/usr/bin/yum --security --exclude\="kernel*" check-update On one server when I enter the command over SSH as follows it works fine. ssh -t -q... (1 Reply)
Discussion started by: scotbuff
1 Replies

5. Homework & Coursework Questions

Shell Script Password Prompt

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to write a shell script that prompts the user for the password which is "lux" once the correct password... (4 Replies)
Discussion started by: Emin_Em
4 Replies

6. Shell Programming and Scripting

How to (GUI) prompt for password from bash?

I remember there was a gnome only command that we could insert in a bash script to mount a Linux disk that would pop up a little window to grab a password. I know there are bash commands to read a string but they are not GUIs and they echo the characters typed. This gnome command popped up a... (1 Reply)
Discussion started by: siegfried
1 Replies

7. UNIX for Dummies Questions & Answers

Sudo -s without password prompt

hi, i have a requirement where i need to sudo to another user in the shell script.suppose consider user A and B, first user A calls a shell script and then i need to sudo to user B which executes another shell script inside the earlier one. also this needs to be automated like while sudo'ing to... (3 Replies)
Discussion started by: krk
3 Replies

8. Shell Programming and Scripting

password in sudo script

salmo allikm warhmat allah wabrakato i want to do script with sudo like sudo su and want to put password in the script not get from user because i to made it startup when booting and i don't know how put in script for sudo thanks (5 Replies)
Discussion started by: pua06
5 Replies

9. Shell Programming and Scripting

sudo, use in script without prompt for password

I need to create an automated script where I have to use sudo to switch to multiple user so the script stops and prompts for password, Is there a way I can provide the password in same command only? Remember that, I cannot disable the password settings of sudo as I dont have rights. (4 Replies)
Discussion started by: gauravgrover50
4 Replies

10. UNIX for Dummies Questions & Answers

sudo in OS X shell script without password prompt??

I've written a shell script to alter a particular preference file on OS X (10.3.9), which works fine (tested by running the script from the terminal sat in front of the box). Problem is, I now have to run this script remotely across a number of machines via remote desktop, so where I've used the... (1 Reply)
Discussion started by: Brad_GNET
1 Replies
Login or Register to Ask a Question