How to force /bin/bash interpreter for oneliners

耗尽温柔 提交于 2019-12-11 07:19:03

问题


I have a one liner bash command that is executed with python os.system()

for FILENAME in mydir/PRE_*; do echo $FILENAME `printf ${FILENAME:0:12}; printf ${FILENAME:23}`; done;

It loops through all files in a directory and renames them, to take chars 12-23 out of the filename. The command works fine when I execute it through bash with my server login, but when I executed it through python with the web user it throws errors.

I found out that this is because the web user's default shell is /bin/sh instead of /bin/bash.

I am able to log in with that user and change the shell to /bin/bash.

So the question is can I force the one-liner to execute with /bin/bash without changing the default shell for the user?


回答1:


Probably you can call it as:

/bin/bash -c '$cmd'

Where $cmd is the full command line.




回答2:


from subprocess import Popen, PIPE, STDOUT

p = Popen(['/bin/bash','-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
cmdout = p.communicate(input='uname -a\nuptime\nprintenv\n')[0]
print(cmdout)


来源:https://stackoverflow.com/questions/20906073/how-to-force-bin-bash-interpreter-for-oneliners

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!