gpt4 book ai didi

linux - 为什么Linux中的 'system'函数运行不了这个shellscript?

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

我遇到了一个关于系统功能的问题。如果我跑

echo -e '\x2f'

在 shell 中,输出是 / 但是当我把命令放在 C 程序中时:

int main(int argc, char* argv[], char** envp)
{
printf("The command is :%s\n",argv[1]);
system( argv[1] );
return 0;
}

输出是:

The command is :echo -e '\x2f'
-e \x2f

为什么system函数输出'-e\x2f'而不是'/'

顺便说一句,我使用 Python 输入 argv:

# I used \\ because python will transfer \x2f to / automatially
command="echo -e '\\x2f'"
p=process(executable='/home/cmd2',argv= ['/home/cmd2',command])
print (p.readall())

最佳答案

首先,echo命令在shbash之间的输出可能不同。
引用:https://unix.stackexchange.com/questions/88307/escape-sequences-with-echo-e-in-different-shells

bash -c "echo -e '\x2f'"
# Output : /
sh -c "echo -e '\x2f'"
# Output : -e /

为了让 Python 吐出同样的东西,像下面这样的东西应该可以工作。
(供您引用,包含与子流程相同的实现)

import os
import subprocess

command = "echo -e '\\x2f'"

os.system( command )
# Output : -e /
subprocess.call( command , shell=True )
# Output : -e /

bashcmd = "bash -c \"echo -e '\x2f'\""

os.system( bashcmd )
# Output : /
subprocess.call( bashcmd , shell=True )
# Output : /

不过,我不确定您是如何将 -e\x2f 作为输出的。

关于linux - 为什么Linux中的 'system'函数运行不了这个shellscript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40668202/

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