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!

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

import os
os.system('enter your bash command here')
os.system('/usr/sbin/useradd brian')

Does doing it like that return a value?

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

>>> os.system('mkdir newdirectory')
0

so you can test for a succesful execution of the command like this

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

>>> os.system('cat grepfile | grep "non-existant string"')
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

>>> os.WEXITSTATUS(os.system('cat grepfile | grep "non-existant string"'))
1

ok, that's pretty cool! Thanks!

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!

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?

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.