help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: simpler question about my script


From: Dennis Williamson
Subject: Re: simpler question about my script
Date: Tue, 15 Feb 2022 17:17:19 -0600

On Tue, Feb 15, 2022 at 2:51 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>
wrote:

> . . .
>
> ill try to recode it readable for other ppl then ask again
>
>


That will go a long way toward making it easier for you as well. Readable
code is easier to understand and maintain and is self-documenting. Using an
editor with syntax highlighting is helpful when looking for syntax errors
and unbalanced delimiters. Auto indenting and code completion and many
other helpful features are also available.

A few tips (some are specific to shell scripts):

Organize code into functions and use clear entry and exit points. Don't
intermingle top level code and function definitions.

Use proper nested indentation to make control statement (for, if, while)
bodies stand out. Don't use one-liners in scripts.

Use meaningful names for variables, functions and other elements. Avoid
single-letter names.

When an external program provides a choice of long or short options, use
the long ones (when portability isn't an issue). Long options improve
readability.

Use if-then-elif-else-fi instead of boolean operators.

Never use aliases in scripts. Use functions instead.

Avoid being clever. Clarity is important. Avoid side effects whenever
possible.

Use variables to hold values instead of hard-coding the values where
they're used. This helps document the code and improves maintainability.
Set a variable to the value of a positional parameter (e.g. user_name=$1)
for the same reasons.

Know when and why to bend the rules, but keep it to a minimum.

Some of these tips are intended to draw a distinction between command-line
use where brevity has value and scripting where clarity is a higher
priority.

There are many other good guidelines you can make use of. These are some
basic ones focused on code clarity. Functionality and performance are
beyond the scope of this message.

Comments are welcome.

Dennis

-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]