问题
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