繁体   English   中英

su -c从引号内的命令中获取参数

[英]su -c taking arguments from command inside quote

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" 

建立

重击:4.3.11

Ubuntu的:14.04.1

这行弄乱了事情:

echo "$($cmd)";

只需将其替换为:

eval "$@";

一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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