为什么 'system' 不能在 Linux 运行 这个 shellscript 中运行?
Why can't the 'system' function in Linux run this shellscript?
我遇到了一个关于系统功能的问题。如果我运行
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
命令在 sh
和 bash
之间的输出可能不同。
参考: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
作为输出的。
我遇到了一个关于系统功能的问题。如果我运行
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
命令在 sh
和 bash
之间的输出可能不同。
参考: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
作为输出的。