ETenal ETenal - 22 days ago 5
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
system
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

Firstly, echo command can output differently between sh and bash.
Ref: http://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 /

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 /
subprocess.call( command , shell=True )
# Output : -e /

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

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

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