python and bash

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 45
Reputation: joey101 is an unknown quantity at this point 
Solved Threads: 0
joey101 joey101 is offline Offline
Light Poster

python and bash

 
0
  #1
Jul 28th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: python and bash

 
0
  #2
Jul 29th, 2005
i am pretty new to python, but are you talking about the os module? So long as your python script is started by bash, you have access to all of your system command using bash. example
[php]
import os
os.system('enter your bash command here')
os.system('/usr/sbin/useradd brian')
[/php]
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 45
Reputation: joey101 is an unknown quantity at this point 
Solved Threads: 0
joey101 joey101 is offline Offline
Light Poster

Re: python and bash

 
0
  #3
Jul 29th, 2005
Does doing it like that return a value?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: python and bash

 
0
  #4
Jul 29th, 2005
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
  1. >>> os.system('mkdir newdirectory')
  2. 0

so you can test for a succesful execution of the command like this
  1. >>> return_value = os.system('mkdir newdirectory')
  2. >>> if return_value == 0:
  3. ... print "it executed without errors"
  4. ...
  5. 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
  1. >>> os.system('cat grepfile | grep "non-existant string"')
  2. 256
it should be giving a return value of 1(not 256), since the string was not found. that can be corrected using os.WEXITSTATUS
  1. >>> os.WEXITSTATUS(os.system('cat grepfile | grep "non-existant string"'))
  2. 1
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 45
Reputation: joey101 is an unknown quantity at this point 
Solved Threads: 0
joey101 joey101 is offline Offline
Light Poster

Re: python and bash

 
0
  #5
Jul 29th, 2005
ok, that's pretty cool! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 18
Reputation: Avner .H. is an unknown quantity at this point 
Solved Threads: 0
Avner .H. Avner .H. is offline Offline
Newbie Poster

Re: python and bash

 
0
  #6
Aug 9th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 1
Reputation: akdom is an unknown quantity at this point 
Solved Threads: 0
akdom akdom is offline Offline
Newbie Poster

Re: python and bash

 
0
  #7
Aug 24th, 2006
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!
The os.system('') command should work fine... the important thing is that you use the proper one-line syntax for the bash shell. Here is an example copied from your list above: os.system('T1="foo"; T2="bar"; if [ "$T1" == "$T2" ]; then echo even; else echo not even; fi')

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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: ovoid is an unknown quantity at this point 
Solved Threads: 0
ovoid ovoid is offline Offline
Newbie Poster

Re: python and bash

 
0
  #8
Aug 1st, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC