Passing an argument to su (Linux).

Leave a comment

January 26, 2017 by aubreykloppers

This one had me a bit baffled.  What I was trying to do was to get a “string” or “argument” and pass this to another user, for example run a script from “root” requiring one of two arguments, either “USERNAME” or “ALL”, then pass it to a user to use in a script.

The format (in bash) is

if [ "$1" == "" ]; then
  echo Please use "$0 [ARG] Where [ARG] can be a user or all."
  echo i.e.: "$0 all"
  echo i.e.: "$0 TOM"
  if [ "$1" == "all" ]; then
    su - <USER> -c 'echo $0' $1
    su - <USER> -c 'echo $0' $1

If you run the above script, you will either get “all” or “TOM” echoed back to you from the user “<USER>” you specify.

Why this madness you ask?  Well, my mail server “Zimbra” does not allow root to interfere with the inner workings of the mail server, thus you can run this kind of script directly from root and pass it to your Zimbra user…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

counter for wordpress
%d bloggers like this: