Ask HN: Can a shell script ever be too big?

4 points by WhackyIdeas ↗ HN
I am working on a PCKSH script which is verging on 5000 lines. It’s purpose is irrelevant, but OpenBSD server installation and configuration.

And it made me wonder… What is the consensus on large scripts? What is the largest script you have coded?

4 comments

[ 2.3 ms ] story [ 66.8 ms ] thread
I didn't code it, but at a prior position part of my job was to maintain a 15,000 line sh script. (Why? Because it was part of a large enterprise product that had to run on a wide variety of platforms. That made a compiled binary problematic for installation and configuration.)

Honestly? It was fine. It was well-written and easy to follow and maintain.

I don't think the size of the script (or source code for a proper programming language) is nearly as important as how well-written it is.

Clarity is more important but so is scope, I think. If the script is trying to do programming language stuff (data structures, complex computation -- as opposed to just calling commands on files) in a command language (like Bash), that's bad. Use a programming language, like Python.

If it's a complex program, switch from a scripting language to a compiled language (since compilers give you static type checking, which is useful in large programs).

> Clarity is more important but so is scope, I think.

Scope is important, yes. My point was really just that very large shell scripts can be done in a way that isn't a nightmare.

I agree that if you're doing more complex stuff such as data structures and the like, using a programming language should be the default position.

In this particular case, it was not possible to use Python as that doesn't exist on most the platforms that we needed to support. Using a compiled language was possible, but not desirable, as it would have greatly complicated everything. A shell script really was the least-bad option.

Once the shell script was done doing its thing, the actual application was compiled (by the script), though. The script wasn't doing the main business logic, only installation and configuration.

As long as the function of the script is clear, I don't see any reason why it should be limited to any particular size at all. If your script is 5000 lines, so be it.

On the other hand, it would probably make sense to break that script into module-sized smaller scripts. The 'base' script would call those 'module' scripts as and when necessary.

Then each 'module' could be debugged and/or modified separately.