Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Switching user inside a shell script Post 302377998 by Scott on Sunday 6th of December 2009 10:03:39 AM
Old 12-06-2009
If you want to run something as another user you either have to have SUDO set up to allow this (by root) or the owner of the other "program" should set the SUID bit.

On many (or maybe all?) systems SUID has no effect with shell scripts.

Code:
$ ls -l Test*
-r-sr-xr-x  1 user2  staff  8800 Dec  6 13:46 TestCProg # C Program
-r-sr-xr-x  1 user2  staff    35 Dec  6 13:19 TestScript # Shell script

$ id user2
uid=502(user2) gid=20(staff)

$ ./TestCProg > /dev/null &; ps -ef | grep TestCProg
[1]	97055
  502 97055 96971   0   0:00.00 ttys000    0:00.00 ./TestCProg
  501 97057 96971   0   0:00.00 ttys000    0:00.00 grep TestCProg

$ ./TestScript &; ps -ef | grep TestScript
[2]	97063
  501 97063 96971   0   0:00.00 ttys000    0:00.00 ./TestScript
  501 97065 96971   0   0:00.00 ttys000    0:00.00 grep TestScript

Another option is to use the Expect tool. This is the first time I've used it, so probably not very good, but...

Code:
/usr/bin/expect << !
spawn su - y
expect "Password:"
send "y_password\r"
send "who am i\r"
send "exit\r"
expect "exit"
!

$ who am i
x        ttys000  Dec  6 15:49
$ ./Test
spawn su - y
Password:
y$ who am i
y        ttys002  Dec  6 15:50 
y$ exit

It should also be possible to use SSH to do what you want, but you'd need to set up the keys the first time
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

switching user from root to ordinary user

Good day Guys!!! I am currently making a script in AIX, the script runs a SAS job, the owner of the script is the root, but the SAS jobs cannot be run by the root, as it should be run by a user 'sasia'. But inside the script, root creates a logfile, so what I need is just to su to sasia for the... (3 Replies)
Discussion started by: sasia
3 Replies

2. Shell Programming and Scripting

switching to another user in shell script...

Hi, I have a shell script in which I need to switch to another user and execute some commands and then come back to the original user. To make it more clear - I have to log in as user root then 'su' to jag - execute a script called backup.sh and then logout and come back to root again.. ... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

3. Shell Programming and Scripting

problem switching to directory using shell script

Hi, I have an issue with switching to other directory through shell script. I have used cd /Music" but that doesn't help me. Then I have also tried using alias proj 'Music' alias then I get the error permission denied but i set the chmod 777 Music. I am using cShell for my... (3 Replies)
Discussion started by: baluk
3 Replies

4. Shell Programming and Scripting

Switch user inside shell script

Hi, I am trying to create one script where I have to login as another user inside the script to exeute some commands How can i achieve this? Many thanks in advance. (4 Replies)
Discussion started by: prarat
4 Replies

5. Shell Programming and Scripting

Login into another user from user inside script

now i have logged in username : ramesh in unix Now i have to created script file to login into another user and have run a command inside that user and after executing the command i have to exit from that user. Inside script, i have to login into su - ram along with password : haihow and have to... (4 Replies)
Discussion started by: rammm
4 Replies

6. Shell Programming and Scripting

How to login as a different user inside a shell script?

hi, i want to login as a different user inside a shell script and then call another shell script from that script. how to do that? original script : script_A.sh so when the script_A.sh is called , i want to login as a different user and then call another shell script(script_B.sh) from... (3 Replies)
Discussion started by: Little
3 Replies

7. Shell Programming and Scripting

Switching user inside a shell script doesn't seem to work

Linux version : Oracle Linux 6.4 Shell : Bash The following script will be run as root. During the execution, it should switch to oracle user and execute few commands. After googling and searching within unix.com , I came up with the following syntax ## Enclosing all commands in double... (7 Replies)
Discussion started by: John K
7 Replies

8. Shell Programming and Scripting

Switch user without password inside shell

I want to switch to another user without password inside shell. I used the below command and it is not working. sudo su - user1 user1 is not in the sudoers file. This incident will be reported. I'm getting the above message. If I want to add user1 into the sudoers file using... (5 Replies)
Discussion started by: Roozo
5 Replies

9. Shell Programming and Scripting

Switching server in UNIX via shell script (SSH )

Requirement: I am writing a shell script which take some file from say Server1 and I have to execute some command on Server2 then I have to manipulate the data and based on that I have to produce report. Problem:I can schedule the shell script on only one server (Server1 or Server2) So, In the... (3 Replies)
Discussion started by: raks2301
3 Replies

10. UNIX for Dummies Questions & Answers

Switching from root to normal user takes me to user's home dir

Whenever i switch from root to another user, by doing su - user, it takes me to home directory of user. This is very annoying as i want to be in same dir to run different commands as root sometimes and sometimes as normal user. How to fix this? (1 Reply)
Discussion started by: syncmaster
1 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 07:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy