Operating System - Linux
1828345 Members
2979 Online
109976 Solutions
New Discussion

Shell Script - Sending commands after "su - ergonom"

 
SOLVED
Go to solution
Ricardo Bassoi
Regular Advisor

Shell Script - Sending commands after "su - ergonom"

I have a doubt how to implement this:

Like the root user I?d like to run a command after do the following

#!/bin/ksh
su - ergonom # Ergonom is a user name
EdnStart # I?d like to run this command after do a su - ergonom

I cannot change the .profile. How can I send any kind of command after do a su - username ?
When I run my simple script the prompt change from the root user ( # ) to the user one ( $ergonom ).
The command EdnStart only runs with the user ergonom and in his home path. This command is a binary file so I don?t have access.
Somebody can help ?
If you never try, never will work
7 REPLIES 7
Santosh Nair_1
Honored Contributor
Solution

Re: Shell Script - Sending commands after "su - ergonom"

You would do this by using the following command:

su - $USER -c $COMMAND

where $USER is the login you want to run $COMMAND under.

-Santosh
Life is what's happening while you're busy making other plans
Sanjay_6
Honored Contributor

Re: Shell Script - Sending commands after "su - ergonom"

Hi Ricardo,

Use this command
su - ergonom -c "type the full command here with the path to that command and any options/switches"

This should work.

thanks
federico_3
Honored Contributor

Re: Shell Script - Sending commands after "su - ergonom"

in order to execute command and its arguments using a temporary environment and permissions of user, you should do the following:

su - user -c "command "


federico
Tony Walker
Frequent Advisor

Re: Shell Script - Sending commands after "su - ergonom"

The syntax mentioned will definitely do the job. If you are still worried about the users .profile you could use something like the following:

su - ergonom -c `. .profile;$COMMAND`

Rgds, Tony
Herve BRANGIER
Respected Contributor

Re: Shell Script - Sending commands after "su - ergonom"


Hi

If you can install (you need root access !!!)
sudo, you can configure your system to launch
commands as another user. For example, you can
configure sudo to launch "ls" as "user2" when
you are "user1".

You can find sudo at :
http://hpux.cict.fr/hppd/hpux/Sysadmin/sudo-1.6.2b1/

Regards,

Herv?

Re: Shell Script - Sending commands after "su - ergonom"

I have run scripts this way successfully. One caveat is the stdout seems to get lost.
Dennis Handly
Acclaimed Contributor

Re: Shell Script - Sending commands after "su - ergonom"

As mentioned by everyone, you need to use: -c command
This is in the fine print of su(1):
The arguments are passed along to the new shell for execution, ...

You then have to read the shell documentation to find out what is valid.

>Tony: If you are still worried about the users .profile you could use something like the following: su - ergonom -c `. .profile;$COMMAND`

You don't need to worry about .profile since you are using "su -". And you should be using (") not (`).