Can we pass some arguments to system("cmdline")?

D

Didier C

Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")

would cause python to execute "ls dir" where "dir" might not exist at
all! Is there a way to reproduce the same thing in Python?

Thanks for any insights.

cheers,
Didier.
 
Q

Qiangning Hong

Didier said:
Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")

would cause python to execute "ls dir" where "dir" might not exist at
all! Is there a way to reproduce the same thing in Python?

Thanks for any insights.

cheers,
Didier.

You should use something like this:

dir = "/home/cypher"
system("ls %s" % dir)

--
Qiangning Hong

_______________________________________________
/ lp1 on fire \
| |
\ -- One of the more obfuscated kernel messages /
-----------------------------------------------
\
\
\
___ _____ ___
/ \ / /| / \
| | / / | | |
| | /____/ | | |
| | | | | | |
| | | {} | / | |
| | |____|/ | |
| | |==| | |
| \___________/ |
| |
| |
 
A

Andreas Kostyrka

Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")
system("ls %(dir)s" % locals())
system("ls %s" % dir)
system("ls %(name)s" % dict(name=dir)

But you should consider if you really really want to do this.
What happens when " " in dir?
What happens when dir == "; rm -Rf /"

Andreas
 
L

Leif K-Brooks

Didier said:
E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

Is there a way to reproduce the same thing in Python?

system("ls %s" % dir)

But you should really be using subprocess for security (so that if
dir=="/home/foo; rm -rf /" nothing bad will happen):

import subprocess
subprocess.Popen(['ls', dir])
 
D

Didier Casse

Thanks all for the reply. I'll try out those things out. :)

Cheers,
Didier.


Leif K-Brooks a écrit :
Didier said:
E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

Is there a way to reproduce the same thing in Python?

system("ls %s" % dir)

But you should really be using subprocess for security (so that if
dir=="/home/foo; rm -rf /" nothing bad will happen):

import subprocess
subprocess.Popen(['ls', dir])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top