Please login or register. December 12, 2019, 03:05:59 PM

Author Topic: run script as another user  (Read 6976 times)

0 Members and 1 Guest are viewing this topic.

fbergenh

  • Senior Member
  • ****
  • Posts: 40
  • Karma: +0/-0
run script as another user
« on: August 29, 2011, 12:51:49 PM »
I know it is a very basic AU13 question, but I am not sure anymore (it is a long time ago that I attended that course  ;) )

What is the difference (if any) between the following commands and what is the correct one to use to run a script as another user:

# su - <user> -c "/<dir>/<script>"

# su - <user> "-c /<dir>/<script>"

Michael

  • Administrator
  • Hero Member
  • *****
  • Posts: 1188
  • Karma: +0/-0
Re: run script as another user
« Reply #1 on: September 06, 2011, 08:48:41 PM »
Well, I do not think I would call it all that "basic" - maybe because I have never used it.

From the man page:

Syntax

       su [ - ] [ Name [ Argument ... ] ]

.... snip ...

 4    To run the backup command with root user authority and then return to your original shell, enter:

            su root "-c /usr/sbin/backup -9 -u"

            This runs the backup command with root user authority within root's default shell. You must give the correct root password
            when queried for the command to execute.


Basically, according to syntax rules, your first command should do unexpected things as it has too many variables. Putting the last arguments between doublw quotes makes it count as a single argument when the command gets executed.

Since su just passes the command to a shell (e.g. ksh) reading the man page for ksh helps to understand the the reason behind the -c part of the command.

From man ksh:
-c String
            Causes the Korn shell to read commands from the String variable. This flag cannot be used with the -s flag or with the
            File[Parameter] parameter.
« Last Edit: September 07, 2011, 12:44:54 PM by Michael »