ETenal ETenal - 1 year ago 77
Bash Question

Why can't the 'system' function in Linux run this shellscript?

I meet a problem about system function. If I run

echo -e '\x2f'

in shell,the output is
but when I put the command in C program like:

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

output is:

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

Why does the
function output '
-e \x2f
' instead of '

BTW, I use Python to input the 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())

Answer Source

Firstly, echo command can output differently between sh and bash.

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

In order to have Python spit out the same, something like below should work.
(For your reference, included the same implementation with subprocess)

import os
import subprocess

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

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

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

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

I am uncertain of how you got -e \x2f as your output though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download