如果使用 sudo 调用,变量分配会失败
我必须从 C++ 命令行应用程序执行类似 m=7
的作业。当我使用 sudo MyApp 运行此应用程序时,程序中存在的命令无法执行。
sudo m=7
失败并出现以下错误
未找到命令
。
有什么方法可以让我使用命令中存在的 sudo 关键字为变量赋值吗? 基本上我想要一种方法来执行sudo {Assignment}
,即sudo m=3
。谢谢。
I have to do an assignment like m=7
from the C++ command line application. When I run this application using sudo MyApp
, the command present in the program fails to execute.
sudo m=7
fails with the following error
Command Not Found
.
Is there any way so that I can assign value to a variable with the sudo
keyword present in the command?
Basically I want a way to do sudo {Assignment}
i.e. sudo m=3
. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sudo
命令允许用户以给定用户(默认:root)运行外部命令。m=7
不是外部命令,因此无法通过 sudo 运行。它是一个变量赋值语句,由当前shell直接解释并执行。Command not found
消息表示 sudo 未能找到名为 m=7 的可执行命令。事实上,很难想象运行 sudo m=7 的预期目标是什么。如果您想将 7 分配给 shell 变量
m
,则不需要任何特殊权限或sudo
,只需运行m=7
即可。如果您想打开 root shell 并执行一些以m=7
开头的命令,只需以sudo bash
开头,然后发出m=7
陈述。The
sudo
command allows one to run an external command as a given user (default: root).m=7
is not an external command and hence cannot be run by sudo. It is a variable assignment statement that is directly interpreted and executed by the current shell.The
Command not found
message indicates that sudo failed to find an executable command named m=7.In fact, it is hard to imagine what the intended goal of running
sudo m=7
might be. If you want to assign 7 to the shell variablem
you do not need any special privileges orsudo
for that, just runm=7
. If you want to open root shell and execute some commands there starting withm=7
just start withsudo bash
and then issue them=7
statement.