0

I have a snippet of a shell script like so -

status() {
        if [ -x "$PRGDIR/qtcat_ctl.sh" ]; then
                echo "Status-ing qtcat Server"

                if [ -z "$QTCAT_RUNTIME_USER" ]; then
                        eval $PRGDIR/qtcat_ctl.sh status
                else
                        su - $QTCAT_RUNTIME_USER -c $PRGDIR/qtcat_ctl.sh status
                fi

                RETVAL=$?
        else
                echo "Startup script $PRGDIR/qtcat_ctl.sh doesn't exist or is not executable."
                RETVAL=255
        fi
}

If QTCAT_RUNTIME_USER is set to "" the script runs fine but if I try to set QTCAT_RUNTIME_USER to another account the status parameter does not get passed to the qtcat_ctl.sh script.

3
  • If the variable does not exists, it is replaced by the empty string, so it does not work when QTCAT_RUNTIME_USER is set to the empty string. The variable is simply never set. Commented Jan 12, 2017 at 16:43
  • actually the problem was not the variable it was the actual command. I had to wrap it in quotes like so: su - user -c "/opt/QASymphony/qtest/qtest/bin/qtcat_ctl.sh status" Commented Jan 12, 2017 at 16:46
  • 1
    Always enclose your variables in double quotes. Commented Jan 12, 2017 at 16:54

1 Answer 1

1

If you need to send an argument to your program via su, you have to quote all the command.

Example with the program a.sh :

$ cat a.sh
echo a.sh $1

$su - $USERNAME -c $PWD/a.sh test
a.sh
$su - $USERNAME -c "$PWD/a.sh test"
a.sh test
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.