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

Posted on 2011-10-11
Last Modified: 2012-08-13

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 '' 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 " 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.
Question by:Hope4U
    LVL 40

    Expert Comment

    but what is in the script ?

    It could be that it does not use the positional parameter $1
    LVL 48

    Accepted Solution

    You need to get rid of the eval.

    Just use

    su - user1 -c " John_Browne"

    Open in new window


    Author Closing Comment


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

    [root@bb]# cat

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

    echo "Hello $1"

    exit 0


    [root@bb]# cat

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


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

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

    echo; echo "DONE!!!"

    exit 0


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


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now