<div dir="ltr">On Thu, Jan 10, 2013 at 7:01 AM, Karim <span dir="ltr"><<a href="mailto:kliateni@gmail.com" target="_blank">kliateni@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<br>
<br>
Hello all,<br>
<br>
I want to run multiline shell command within python without using a command file but directly execute several lines of shell.<br>
I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but I want to know if it is posssible to avoid making file and execute<br>
shell lines of code directly.<br>
<br></blockquote><div><br></div><div style>Yes, this is very possible. Specify shell=True as an argument and you can do anything you can do in a shell:</div><div style><br></div><div style> >>> commands = """echo hello</div>

<div>... echo hello | wc -l</div><div>... ps aux | grep python"""</div><div>>>> b = subprocess.check_output(commands, shell=True)</div><div>>>> print(b.decode('ascii'))</div><div>
hello</div>
<div>1</div><div>hugo      1255  1.0  0.6 777316 49924 ?        Sl   09:14   0:08 /usr/bin/python2 /usr/bi</div><div>hugo      6529  0.0  0.0  42408  7196 pts/0    S+   09:23   0:00 python</div><div>hugo      6559  0.0  0.0  10656  1128 pts/0    S+   09:28   0:00 grep python</div>

<div><br></div><div>>>> </div><div><br></div><div style>watch out though, accepting user input into the commands variable will lead to shell injection, which can be a dangerous security vulnerability.</div><div>
<br>
</div><div style>HTH,</div><div style>Hugo</div></div></div></div>