繁体   English   中英

shell脚本回显在sudo内部不起作用

[英]shell scripting echo not working inside sudo

我有一个shell脚本test.sh ,如下所示。

sudo -H sh -c '
    echo $1;    
'

但是,当我以./test.sh abcd运行此脚本时,它没有回显任何内容。 然后,我按如下所示更改了脚本。

sudo -H sh -c '
    echo \$1;    
'

但是现在,它将输出显示为$1 为了获得abcd输出,我需要在这里进行什么修改。 请指教,因为我是shell脚本的初学者。

谢谢。

尝试这个:

sudo -H sh -c "
  echo $1;    
"

sudo在新的外壳程序中运行命令,该外壳程序不知道传递给其父级的参数,因此您需要先扩展它们,然后才能在新的(子)外壳程序中运行命令字符串。

尝试:

sudo -H sh -c '
echo "$1";    
' argv0 "$1"

bash手册页:

man bash | less -Ip '-c string'

# -c string If the -c option is present,  then  commands  are  read  from
# string.   If  there  are arguments after the string, they are
# assigned to the positional parameters, starting with $0.

暂无
暂无

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

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