views:

154

answers:

4

I decided to rewrite all our Bash scripts in Python (there are not so many of them) as my first Python project. The reason for it is that although being quite fluent in Bash I feel it's somewhat archaic language and since our system is in the first stages of its developments I think switching to Python now will be the right thing to do.

Are there scripts that should always be written in Bash? For example, we have an init.d daemon script - is it OK to use Python for it? We run CentOS.

Thanks.

+3  A: 

It is OK in the sense that you can do it. But the scripts in /etc/init.d usually need to load config data and some functions (for example to print the nice green OK on the console) which will be hard to emulate in Python.

So try to convert those which make sense (i.e. those which contain complex logic). If you need job control (starting/stopping processes), then bash is better suited than Python.

Aaron Digulla
+1 (solid answer)
ChristopheD
+2  A: 

Generally, scripts in /etc/init.d are written in the "native shell" of the OS (e.g. bash, sh, posix-sh, etc). This is especially true of scripts that will be run at the lower init levels (e.g. not every directory will be mounted in single user mode, including wherever python or the site-libraries might be installed).

Most OS's provide some "helper functions" that make writing scripts in some native shell easier. These scripts define certain return codes and messages that are required/desired when writing service scripts. On RedHat based systems, see:

/etc/init.d/functions 

Beyond that, the service scripts in /etc/init.d can be written in any language (including compiled languages). The general calling syntax will need to be supported. Typically there are three arguments that should be supported: start, stop, and status. Some additional arguments might be appropriate, depending on the purpose of the scripts.

% /etc/init.d/foo (start|stop|status)  
semiuseless
+1 for pointing out that Python may not be available during boot, for mentioning init script shell functions, and for explaining the requirements on init scripts. On many POSIX systems, Python is installed below /usr/ which may be on a separate partition. Another example for an init script function library is /lib/lsb/init-functions from the Linux Standard Base, which is available on Debian, Ubuntu, SUSE, and probably many other Linux systems.
cj
+1  A: 

Every task has languages that are better suited for it and less so. Replacing the backtick ` quote of sh is pretty ponderous in Python as would be myriad quoting details, just to name a couple. There are likely better projects to cut your teeth on.

And all that they said above about Python being relatively heavyweight and not necessarily available when needed.

msw
A: 

Certain scripts that I write simply involving looping over a glob in some directories, and then executing some a piped series of commands on them. This kind of thing is much more tedious in python.

fmark