Right Ingredients
Does the ingenious script your wrote last month look like confusing spaghetti code today? We show some proven style guidelines that can provide order and readability.
|
Does the ingenious script your wrote last month look like confusing spaghetti code today? We show some proven style guidelines that can provide order and readability.
Bash scripts help rename files, sort a large address list, or execute other similarly tedious tasks. Scripts created under pressure can often look garbled (see Listing 1 for a prime example). Quick hacks may have their place, but they are often confusing and might be totally unclear a week later. For this reason, many users don't share their scripts, feeling they are not up to par. All this contributes to making such scripts hard to expand or change down the road.
Listing 1
Unformatted Script
#!/bin/sh a="100"; b="3"; c="."; d="shot"; for i in `seq 1 $a`; \ do import -window root $c/$d$i.png; sleep $b; done; exit
For these reasons it's best to structure, format and, above all, comment your Bash scripts from the outset. Some guidelines can be helpful, such as the one from Google [1] or the handy PDF booklet written by Fritz Mehner [2] that you can print and keep by you computer (Figures 1 and 2). Whether you choose a commercial style guide or one from academia, the basic guidelines are the same.
Your script should always identify the shell it is using at the beginning. If the script uses Bash functions, include #!/bin/bash
on the first line. That way, systems that don't use Bash as the default shell, but have it installed, can still use it.
To make the script more readable, get used to using the proper indentation and including blank lines. The confusing elements in Listing 1 become less cryptic if presented as in Listing 2.
Listing 2
Script with Proper Indentation
#!/bin/bash a="100" b="3" c="." d="shot" for i in `seq 1 $a` do import -window root $c/$d$i.png; sleep $b done; exit
Each line has a separate instruction and a blank line separates blocks of related code, such as the block for the for-do
loop. The statements in a block should be indented, such as the do
portion of the loop. If you use space characters or a tab for indentation, different editors might interpret them differently. Style guides almost unanimously suggest using a double space character in place of a tab.
Notice that the
do
command is by itself on a line. Google, however, suggests including it on the previous for
line. The same goes for
while
loops and the then
in an if
block. The else
,
however, is often best placed on its separate line, as is the ending done
. As for
if
statements, Google recommends replacing test
with double square brackets as abbreviations:
if [[ ${filename} == "image*" ]]; then ...
This notation has the advantage of allowing for the use of regular expressions. It also reduces errors in that the double square brackets neither arbitrarily truncate words nor expand filenames.
Pages: 4
The Shell Script Compiler converts scripts into binaries, which protects against accidental changes but also carries some pitfalls.
A simple script is usually enough to automate a knotty task in Linux, but with some simple shell commands and a little effort, you can convert a simple batch file into a robust shell program.
The JavaScript language has developed into an important programming language. We explain the basics and provide examples for accessing an open HTML page through the DOM interface.
With just a few lines of shell code, you can build a flexible and easily adaptable stream player.
The ultra-slim web server HTTPie turns out to be an elegant synthesis of curl and wget, making it ideal for use with pipes.
© 2024 Linux New Media USA, LLC – Legal Notice