Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Can't get a script to run properly with command line parameters when using ' eval su - user1 -c"Hello.sh John" '


PROBLEM:  Want to get a sub-process that takes parameters, to run properly when it is called by a parent process, and this parent process is being executed by a different user to the one running the sub-process:

e.g. Script 'Hello.sh' that must be run as user 'user1':

echo "Hello $1"

e.g Script 'm_Script' that is to be run by user 'root'

eval su - user1 -c "Hello.sh John_Browne"

I can't get the above to work properly, 'm_Script' just prints 'Hello', not 'Hello John_Browne''.

[1] This is just a simple breakdown of what I actually have to do, so please don't tell me I can run everything as user root.
[2] If there is another approach I can use that would allow me to pass information (parameters) to a sub-process that is to be run by a different user, please tell me how.
[3] Execution of the main program must wait until the sub-process is finished.
1 Solution
but what is in the script Hello.sh ?

It could be that it does not use the positional parameter $1
You need to get rid of the eval.

Just use

su - user1 -c "Hello.sh John_Browne"

Open in new window

Hope4UAuthor Commented:

[root@bb]# ll -h *.sh
-rwxrwx---    1 user1     user1          119 Oct 11 16:50 Hello.sh
-rwxrwx---    1 root     root          335 Oct 11 17:00 m_Script.sh

[root@bb]# cat Hello.sh

# PURPOSE: To say hello to an individual.  Uses the 1st command line parameter.

echo "Hello $1"

exit 0


[root@bb]# cat m_Script.sh

# PURPOSE: To call another shell script that takes parameters and runs with a different user.


echo "Executing [1]"
eval su - asy -c "${WRK_DIR}Hello.sh John_Brown1"

echo "Executing [2]"
su - asy -c "${WRK_DIR}Hello.sh John_Brown2"

echo; echo "DONE!!!"

exit 0


[4] Output:
[root@bb]# ./m_Script.sh
Executing [1]
Executing [2]
Hello John_Brown2


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now