Run SAME SCRIPT as different user

Dear all,

i am doing an SVN backup script. Almost done.

My problem is :
Script should run by user : svn

There is a chance to run the script by root itself.. so i coded like following

#This script path and name                                                                                                   
if [ `dirname $0` == "." ] ; then
thisScript=`pwd`"/$0"
else
thisScript=$0
fi
echo -e "$thisScript"


if [ $USERNAME == "svn" ] ; then
    FUN_CHECKPRIVILEGES
    if [ $PRIVILEGE == "true" ] ; then
        FUN_BACKUP
    else
        FUN_LOG "!!!---------Backup cannot be done.-----------!!!"
        FUN_LOG "Please check the following :"
        FUN_LOG "1. Permission for user $USERNAME to access all $ALLDIR"
        FUN_LOG "2. Script may be running by any user other than 'svn' or 'root'"
        FUN_LOG "\nExiting ENTIRE BACKUP PROCESS."
    fi
else
    FUN_LOG "changing user to 'svn' and running the script"
    su svn -c 'sh $thisScript'
    FUN_LOG "Exiting."
    exit 1
fi
}

But su svn -c 'sh $thisScript' is not working correctly.

i tried all the following:

su svn -c 'sh $thisScript' --> Will go to bash$ prompt
su svn -c '$thisScript' --> Nothing will do
su - svn -c 'sh $thisScript' --> Nothing will do
su -c 'sh $thisScript' - svn --> Nothing will do

and

su svn -c 'echo "hi hi"' --> is working fine

:confused:

any idea ?

.

You've put single quotes round the 'sh $thisScript' part, that prevents the variable ($thisScript) from being replaced with the contents. You be actually running "$thisScript" instead of the name of the script *stored* in $thisScript.

Try double quotes instead

su svn -c "sh $thisScript"
1 Like

yes.. its working fine..

Thanks a lot.