简体   繁体   中英

su -c taking arguments from command inside quote

CODE

repoPath=/var/www/vhosts/www
user=www-data

showCommands=1

execCmd()
{
    local cmd="$@";

    if [ -n "$showCommands" ]; then
        log "$cmd" "CMD";
    fi

    if [ ! -n "$noExecute" ]; then
        echo "$($cmd)";
    fi
}

suCmd()
{
    echo "$(execCmd su - $user -c \"$@\")";
}

log()
{
    if [ -z $2 ]; then
        level="INFO";
    else
        level=$2;
    fi

    echo -e $(date +%F\ %T) "[$level] $1";
}

main()
{
    echo "$( suCmd git -C ${repoPath} rev-parse --abbrev-ref HEAD )";
}

main

OUTPUT

su: invalid option -- 'C'
Usage: su [options] [LOGIN]

Options:
  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

2014-12-24 18:32:34 [CMD] su - www-data -c "git -C /var/www/vhosts/www rev-parse --abbrev-ref HEAD" 

BUILD

Bash: 4.3.11

Ubuntu: 14.04.1

This line messes up things:

echo "$($cmd)";

Just replace it by that:

eval "$@";

and everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM