gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:06 25 4
gpt4 key购买 nike

代码

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

输出

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"

构建

bash :4.3.11

Ubuntu:14.04.1

最佳答案

这一行把事情搞砸了:

echo "$($cmd)";

只需将其替换为:

eval "$@";

一切正常。

关于linux - su -c 从引号内的命令获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com