Preserving variable in script run with sudo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserving variable in script run with sudo
# 1  
Old 05-15-2013
Question Preserving variable in script run with sudo

In a nutshell, I want $USER to reflect my user ID in a script run with sudo. I'm working with OSX in case that makes a difference.

If I 'sudo echo $USER', I get my user ID. But if I 'sudo myscript.sh' and myscript.sh has a line to echo $USER, I get 'root' I'm hoping there's a switch I can add to the shebang in the script to allow env variables to creep in.

I'm aware of 'sudo -E', but A) that doesn't work, as the script is still running in a new process spawned by root; and B) that wouldn't work as it would require someone running the script to know to use that switch.
# 2  
Old 05-15-2013
Try this when running script with sudo:
Code:
ps --no-headers -o user -p `ps --no-headers -o ppid -p $PPID`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable not preserving value

Hi I am running this shell script .But some how the flag value is getting reset to 0 .could you please help .I'm pasting the output of the script also for your reference. #!/usr/bin/sh # Shell script to monitor or watch the disk space # It will send an email to $ADMIN, if the (free... (5 Replies)
Discussion started by: ptappeta
5 Replies

2. Shell Programming and Scripting

Chmod working in sudo run script but chown isn't

My git user has permission in sudoers to run a wrapper script to move files into my webroot. Everything is working fine except for the chown line. After the script has run, the files ar still root:root instead of apache:apache. Scratching my head...:confused: #!/bin/sh echo echo "****... (4 Replies)
Discussion started by: dheian
4 Replies

3. Shell Programming and Scripting

Variable not an identifier when script is run as another user

I am new to scripting I keep getting the error var2= is not an identifier when I run this script as another user. BUT when I run it as myself, the script completes without error. Any idea why? I assume it is because the new user has a different environment . How do I make the variables active... (5 Replies)
Discussion started by: newbie2010
5 Replies

4. Shell Programming and Scripting

How to run sudo commands under a script?

Hi, I am new to scripting. I am trying to write a script to ssh one remote machine and run a sudo command. ssh <hostname> sudo -S <command> < ~/pass.txt I am stored my password in pass.txt. I am getting error sudo: no tty present and no askpass program specified Please suggest me how can... (1 Reply)
Discussion started by: venkia9
1 Replies

5. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi, Have a need to run the below command as a "karuser" from a java class which will is running as "root" user. When we are trying to run the below command from java code getting the below error. Command: sudo -u karuser -s /bin/bash /bank/karunix/bin/build_cycles.sh Error: sudo: sorry,... (8 Replies)
Discussion started by: Satyak
8 Replies

6. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi All, I running a unix command using sudo option inside shell script. Its working well. But in crontab the same command is not working and its throwing "sudo: sorry, you must have a tty to run sudo". I do not have root permission to add or change settings for my userid. I can not even ask... (9 Replies)
Discussion started by: Apple1221
9 Replies

7. Shell Programming and Scripting

variable has no value if run the script automatically/scheduled

Hi All, I am using ksh as the shell. I have a script that should store variable after executing "onstat" on informix. I can execute it successfully without any issue if run manually, however the value is not showing up when run automatically(scheduled). Is there anything needed to make the... (1 Reply)
Discussion started by: tungaw2004
1 Replies

8. AIX

Run sudo to another user

Run sudo to another user . I have two users in my AIX system appadmin (Application admin ) and appuser (Application user) Application Admin user has special permission to run every thing about this application. I have one script called /usr/app.sh Only appadmin has permission to run this... (11 Replies)
Discussion started by: Mr.AIX
11 Replies

9. Shell Programming and Scripting

Question on tweaking the PATH variable to allow the world to run my executable script

All, I am pretty new to Unix and still in the learning curve :) I have a simple requirement for which I did not get an answer yet (Atleast I do not know how to keyword the search for my requirement!!!). I have an executable script my.script1 in a folder /data/misc/scripts/dev, which when... (5 Replies)
Discussion started by: bharath.gct
5 Replies

10. UNIX for Dummies Questions & Answers

sudo not allow User to run sed

Hello, As recommended by the moderators I will start my questions here... I am a little confused by my Linux (CentOS/RHEL) distribution's sudo mechanism. Or perhaps I just do not know how to use sudo. I can run a sed command as the root user but it fails to write a root-owned directory... (6 Replies)
Discussion started by: duderonomy
6 Replies
Login or Register to Ask a Question