Featured image

Table of Contents Link to heading

Shell Script Link to heading

a file of executable commands stored in a text file.

  • When a shell script is run, each command within it is executed line-by-line.
  • Scripting - the process of building a script - helps to automate repetitive tasks.
  • A good script can save time, provide consistency, and reduce the potential for errors in repetitive processes.

Shebang Link to heading

or crunchbang, a prefix that specifies the absolute path to the interpreter and marks the lines following it as executable (e.g., #!/bin/bash, #!/bin/sh).

There are two ways a script can be run:

  1. Directly
    • ./script
    • If the script is marked as executable, it will be run under the interpreter specified in the shebang line.
  2. Invoked as an argument to an interpreter
    • bash script
    • The interpreter given as the command will be used, no matter what is in the shebang line.

Permission Denied Error Link to heading

If a “Permission denied” error is generated, the script has not been marked as executable. The chmod command can be used to alter the access permissions of a file or directory so that the file can be executed.

Read more at ๐Ÿ”—

Text Editor Link to heading

allow the user to edit shell scripts which are written using plain text.

  1. The GNU nano editor is a simple editor well-suited for editing small text files.
  2. The Visual Editor, vi, or its newer version, VI improved vim, is a remarkably powerful editor.

Variable Link to heading

allow a value stored in the system’s memory is referenced by a human-readable name, hence its easy access and retrieval.

Variable Assignment Link to heading

used to define a variable by assigning a value to it.

  • Form: variable=value
  • To access the contents of a variable, prefix it with a dollar sign $.
  • When the interpreter sees the dollar sign, it recognises that it will be substituting the contents of the variable, which is called interpolation.

Variable Example1

Using Backtick Characters Link to heading

Use the output of another command as the contents of a variable by enclosing the command in backtick characters.

Variable Example2

Using the read Command Link to heading

Get input from the user running the script and assign that input to a variable using the read command.

Variable Example3

Using Arguments Link to heading

Use special variables, which are arguments passed to the script when running it, as the contents of a variable.

  • Special variables are prefixed with a dollar sign $.
  • $0 references the name of the script.
  • $1 references the first argument passed to the script

Variable Example4

Conditional Statement Link to heading

allow different sections of code to be executed depending on tests, called branching.

The generic form of the if statement is:

ConditionalStructure

The test Command Link to heading

provides easy access to comparison and file test operators.

  • if test -f /usr/bin/python; then tests if a file exists
    • Equivalent to if [[ -f /usr/bin/python ]]; then
  • if test -d /tmp; then tests if a directory exists
    • Equivalent to if [[ -d /tmp ]]; then
  • if test 1 -eq 2; then tests if the first integer is equal to the second
    • Equivalent to if [[ 1 -eq 2 ]]; then

The case Statement Link to heading

Simplifies complex conditionals when there are multiple different branches.

The generic form of the case statement is:

CaseStructure

  1. Starts off with a description of the expression being tested:
    • case EXPRESSION in
  2. Next, each set of tests are executed as a pattern match terminated by a closing parenthesis.
    • condition)
  3. Following that are the commands to be executed if the pattern returns true, which are terminated by two semicolons.
    • statement
      ;;
    • One semicolon is used to separated subsequent commands.
    • Commands do not need to be terminated by two semicolons if they belong to the “last resort” clause - indicated by an asterisk character.
  4. Finishes off with an esac statement.
    • esac

The following script illustrates how the case statement works/

Case Example

Run CaseStructure

Loop Link to heading

allow code to be executed repeatedly.

There are two main loops in shell scripts:

  1. A for loop will repeat a specific number of times, either determined by the script or by user input.
    • This loop “counts” the number of times the content will be evaluated or executed.
    • For Loop
  2. A while loop will repeat if the condition evaluated is TRUE.
    • If the evaluation fails on the first check, the loop will never start.
    • While Loop
  3. An until loop will repeat if the condition evaluated is FALSE.
    • If the evaluation fails on the first check, the loop will never start.
    • Until Loop