My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

Learning Python For Bash Scripters

It turns out it is a huge secret that Python is easier to program in and more powerful than Bash. These code examples are meant to help solve that problem. If you feel intimidated by Object Oriented programming, don't, it is optional in Python. You don't even need to know Bash that well, Python is easy, jump right in!

There are five scripts in the subversion repository, the first four scripts produce identical output and do identical things in Python and Bash. The last script is a good example to use as a template to create command line tools for Bash programmers. You can add more commands and add command line options to run them.

If you like it, want to contribute, or just want to talk, send me an email.

Using skeleton code clbash.py:

For the command line tool/template, here is an example usage:

Usage: py4sa [option]

A unix toolbox powered by Python

Options:
  --version      show program's version number and exit
  -h, --help     show this help message and exit
  -i, --ip       gets current IP Address
  -u, --usage    gets disk usage of homedir
  -v, --verbose  prints verbosely
[root@cent][H:2404][J:0]> ./clbash.py -i
10.0.1.2
[root@cent][H:2406][J:0]> ./clbash.py -i -v
IPADDR: 10.0.1.2

If you examine the source code, or just grep for #REPLACE ME, you will see a spot to customize code. Here is an example of uncommenting the lines under #REPLACE ME

[root@cent][H:2417][J:0]> ./clbash.py       
Usage: py4sa [option]

A unix toolbox powered by Python

Options:
  --version      show program's version number and exit
  -h, --help     show this help message and exit
  -i, --ip       gets current IP Address
  -t, --tail     reports tail of system log
  -u, --usage    gets disk usage of homedir
  -v, --verbose  prints verbosely
[root@cent][H:2418][J:0]> ./clbash.py --tail
Jan  2 01:23:04 cent smartd[3731]: Device: /dev/hdc, 8 Currently unreadable (pending) sectors 

Woops, looks like my disk is failing....enough wiki writing...

Powered by Google Project Hosting