| | |
python and bash
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 45
Reputation:
Solved Threads: 0
I'm working on making a server control panel in python (like cpanel, plesk, etc) and wanted to know if any one knew of some tutorials on using bash and python together for creating users and stuff on a *nix system.
Also if any one knows of a tutorial on using paypal with python that would be sweet!
thanks!
Also if any one knows of a tutorial on using paypal with python that would be sweet!
thanks!
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
sure it returns the output of the command, and also any errors that bash produces. If the command used does not usually give output, it will still return the exit status of the command. for example
so you can test for a succesful execution of the command like this
here is something you need to watch out for. an exit stauts of a command execution of other then 0, will be changed using python. So you need to use this module os.WEXITSTATUS
for example. if using the grep command on a linux system, and the value is not found, it will return an exit status of 1. Using this with the os.system module with python will change it. here is an example
it should be giving a return value of 1(not 256), since the string was not found. that can be corrected using os.WEXITSTATUS
Python Syntax (Toggle Plain Text)
>>> os.system('mkdir newdirectory') 0
so you can test for a succesful execution of the command like this
Python Syntax (Toggle Plain Text)
>>> return_value = os.system('mkdir newdirectory') >>> if return_value == 0: ... print "it executed without errors" ... it executed without errors
here is something you need to watch out for. an exit stauts of a command execution of other then 0, will be changed using python. So you need to use this module os.WEXITSTATUS
for example. if using the grep command on a linux system, and the value is not found, it will return an exit status of 1. Using this with the os.system module with python will change it. here is an example
Python Syntax (Toggle Plain Text)
>>> os.system('cat grepfile | grep "non-existant string"') 256
Python Syntax (Toggle Plain Text)
>>> os.WEXITSTATUS(os.system('cat grepfile | grep "non-existant string"')) 1
•
•
Join Date: Aug 2005
Posts: 18
Reputation:
Solved Threads: 0
Hello everyone!
I want to use bash from python, however the os.system(command) seems to only act on one command at a time.
I want to use complete commands, also containing control flow statements, like If and While, and to call them from python.
something like this code in bash:
T1="foo"
T2="bar"
if [ "$T1" == "$T2" ]; then
echo even
else
echo not even
fi
I tried to use os.system and separate the commands with ';' , and it works only on simple one line commands, but not on control flow commands.
any suggestions?
thanks!
I want to use bash from python, however the os.system(command) seems to only act on one command at a time.
I want to use complete commands, also containing control flow statements, like If and While, and to call them from python.
something like this code in bash:
T1="foo"
T2="bar"
if [ "$T1" == "$T2" ]; then
echo even
else
echo not even
fi
I tried to use os.system and separate the commands with ';' , and it works only on simple one line commands, but not on control flow commands.
any suggestions?
thanks!
•
•
Join Date: Aug 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
T1="foo"
T2="bar"
if [ "$T1" == "$T2" ]; then
echo even
else
echo not even
fi
I tried to use os.system and separate the commands with ';' , and it works only on simple one line commands, but not on control flow commands.
any suggestions?
thanks!
That should give you the output of:
not even
0
Just remember to put spaces in the right spots and semi-colons in the right spots... bash gets cranky otherwise.
A helpful hint on how to get this to work properly is to type what you want in regular new-line format into the shell, then look at "previous commands" (history)... if statements are re-formatted to one-line for easy access.
That Help At All?
Last edited by akdom; Aug 24th, 2006 at 11:07 pm. Reason: typo
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
That help me a lot! Thanks...
I am new to linux - and to bash to - and I was'nt now what is wrong with things like ["$a"="$b"]. Now it works fine from bash and python too! ...Just 1 more question: how do I solve sudo paswword from python? Let say os.system('sudo cmd').Wat flag do I need and where I put the pass?
I am new to linux - and to bash to - and I was'nt now what is wrong with things like ["$a"="$b"]. Now it works fine from bash and python too! ...Just 1 more question: how do I solve sudo paswword from python? Let say os.system('sudo cmd').Wat flag do I need and where I put the pass?
![]() |
Similar Threads
- Why should i learn Python? (Python)
- python with bash shell (Python)
- How to use 'top' using Python (Python)
- python and shell scripting (Python)
Other Threads in the Python Forum
- Previous Thread: urlencode / urlencode php + python
- Next Thread: Help with RE module.
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime print program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython





