Shell Programming  «Prev  Next»
Lesson 7 Shell script, other script, or compiled program
Objective Reasons for choosing one programming method

When to use a Shell Script or Compiled Program

As a Unix Administrator, deciding whether to use a shell script or another tool depends on several factors. Here are key guidelines to consider:
  1. Complexity of the Task
    • Simple, repetitive tasks: Use a shell script (e.g., automating backups, cleaning log files).
    • Complex logic or large-scale processing: Consider a higher-level programming language like Python or Perl.
  2. Performance Considerations
    • Shell scripts: Suitable for lightweight automation but can be slow for large data manipulation.
    • Compiled languages (e.g., C, Go): Useful for high-performance needs.
    • Tools like awk, sed, grep, or xargs: Optimize text processing within scripts.
  3. Portability
    • Shell scripts (Bash, Ksh, etc.): Work across Unix-based systems, but variations exist.
    • Cross-platform tools (Python, Perl): Better if portability between Unix and Windows is needed.
  4. Maintainability and Readability
    • Shell scripts: Easy for Unix admins but can become difficult to maintain in large, complex cases.
    • Configuration management tools (Ansible, Puppet, Chef): Better for managing configurations across multiple servers.
  5. Security Concerns
    • Shell scripts: Risk of command injection if handling user input improperly.
    • Compiled languages or tools with better input validation: More secure for sensitive operations.
  6. Integration with System Tools
    • Shell scripts: Ideal for system administration tasks involving cron, systemctl, iptables, etc.
    • Specialized tools: Use package managers (yum, apt), monitoring tools (Nagios, Prometheus), or configuration management systems.
  7. Error Handling and Debugging
    • Shell scripting: Limited error handling, though trap, set -e, and set -o pipefail can help.
    • Python/Perl: Better for robust error handling and logging.
  8. Concurrency Needs
    • Shell scripting: Limited background processing using & or nohup.
    • Multi-threaded languages (Python, Rust, Go): More effective for parallel processing.
  9. Availability of Existing Tools
    • Existing Unix utilities (rsync, sed, awk, grep) might already perform the needed task efficiently.
    • Writing a custom shell script is better if no pre-existing tool fits the exact need.

Conclusion: Use shell scripts for simple automation and Unix system tasks but switch to Python, Perl, Ansible, or compiled languages when dealing with complex, secure, or high-performance requirements.

List reasons for choosing one programming method over another

In this module you have read about several methods for completing a programming project. Each method or tool is best used for a particular circumstance. It is up to you to determine which tool to use based on the problem you need to solve. Below are some guidelines to consider when deciding if a shell script or another tool is the right way to solve a problem.
  1. Does the program need to be able to run on a variety of systems, some of which may have a minimal installation or not yet have all programs configured?
    A shell script is the best solution.
  2. Is it important that the program run quickly because it is complex or runs repeatedly? A compiled language such as C is best.
  3. Do you already have parts of the program functionality available in one language? Consider using the same language to finish the project or, if necessary because of the program's design, use another language for some parts of the project after determining how difficult it will be to interface between the two languages used for the project.
  4. Is the task at hand something that could be done from a UNIX command line but needs to be automated? A shell script is probably the appropriate tool.
  5. Do you require a graphical interface for the project? Use a tcl or Python script, or a compiled language with additional graphical tools.
  6. Do you need to complete the project quickly?
    A shell script may be the best solution, or another scripting language if a shell script does not include all the functionality you need.
  7. Does the project involve networking or low-level computation? A compiled language like C is probably best; extensions to Perl and Python can do most of these tasks but are less efficient than a compiled language.

As you can see, the considerations are as numerous as the tools.
Shell scripts are the best tool for the job in some cases; in others, they just are not up to the task.

Different Unix Programming Tools for the job

The correct matches are:
  1. ${#variable}: Return the length of the variable (number of characters)
  2. ${variable}: Return the value of the variable (method used to avoid ambiguous variable naming)
  3. $variable: Return the value of the varialbe (standard method of referencing a variable)
  4. ${variable:-response}: Return response if the variable is not set
  5. ${variable:=response}: Return response if the variable is not defined and set the variable equal to response
  6. ${variable:?response}: Return response as an error messge if the variable has not value.
  7. ${variable:+response}: Return response if the variable is set, otherwise return a blank string.

Variables are named storage that can hold values. The shell expands a variable when the variable's name occurs after a dollar sign ($), replacing the dollar sign and variable name with the contents of the variable. This is called variable expansion, or substitution, or occasionally replacement or even interpolation. Of these terms, expansion is used most often in documentation, but substitution is probably the clearest. Variables are assigned using an equals sign (=) with no spaces around it:
$ name=John
$ echo hello, $name
hello, John

Unlike most other programming languages, the shell uses different syntax when referring to a variable than when assigning a value to it. Variable names start with a letter or underscore, followed by zero or more letters, numbers, and underscores. Some shell programmers use all capitalized names for variables by convention. In this course, I use all capitalized names only for environment variables or special predefined shell variables (such as $IF,).Do not use mixed case; it works, but it is not idiomatic for the shell. If a variable has not been set, it expands to an empty string; there is no warning (usually) for trying to use an unset variable, which can make it hard to detect simple typos. Variables in shell are always strings; the shell does not distinguish between strings, integers, or other data types. The shell is generally considered a typeless language.
In the next lesson, the module will be summarized.

SEMrush Software 7 SEMrush Banner 7