Lection 1: Bash scripting

Table of Contents

slides
home

1. When not to use shell scripts:

  • Resourse intensive task
    especially where speed is a factor (sorting, hashing, recursion, etc)
  • Math operations
    Especially floating point arithmetic, arbitrary precision calculations or complex numbers
  • Situations where security is important
  • Extensive file operations required (Bash is limited to serial file access)
  • Need native support for multi-dimensional arrays
  • Need data structures, sucha as linked lists or trees
  • Need GUI
  • Need direct access to sustem hardware or external peripherals
  • Need port or socket I/O
  • Need to use llibraries or interface with lecacy code

2. She-bang

The #! is called she-bang, #!/bin/bash is the first line in all shell scripts, you can never be sure chich shell a user is running, a script that works flawlessly in bash might not work in ksh, csh or dash, to instruct a shell to run your script in a certain shell, you can start your script with a she-bang followed by the shell it is supposed to run in. This script will run in a bash shell:

        #!/bin/bash
        echo hello world

3. Bash script vs sourcing a Bash script

When a script is executed it contains its own shell, so the variables do not survive the end of the script:

        #!/bin/bash
        var1=3
        echo var1=$var1
        [student@localhost ~]$ ./simple_variable_in_script
        var1 = 3
        [student@localhost ~]$ echo $var1
        [student@localhost ~]$

But using sourcing:

        [student@localhost ~]$ source ./simple_variable_in_script
        var1 = 3
        [student@localhost ~]$ echo $var1
        3
        [student@localhost ~]$

The differences are:

Script sourcing
When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed. When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.

4. Troubleshooting a script

Another way to run a script in a separate shell is by typing bash with the name of the script as a parameter.

    student@localhost ~]$ bash simple_variable_in_script
    var1 = 4

Expanding this to bash -x allows you to see the commands that the shell is executing (after shell expansion).

    [student@localhost ~]$ bash -x simple_variable_in_script
     var1=4
     echo var1 = 4
    var1 = 4
    [student@localhost ~]$ cat simple_variable_in_script
    #!/bin/bash
    #
    # simple variable in script
    #
    var1=4
    echo var1 = $var1
    [student@localhost ~]$

Notice the absence of the commented (#) line, and the replacement of the variable before execution of echo

5. Conditions and loops

5.1. Test

The test command can test whether something is true or false. Let’s start by testing whether 10 is greater than 55.

    $ test 10 -gt 55 ; echo $?
    1

The test command returns 1 if the test fails. And as you see in the next code, test returns 0 when a test succeeds.

    $ test 56 -gt 55 ; echo $?
    0

If you prefer true and false, then write the test like this.

    $ test 56 -gt 55 && echo true || echo false
    true
    $ test

The test command can also be written as square brackets, the code below is identical to the one above.

    $ [ 56 -gt 55 ] && echo true || echo false
    true
    $ [ 6 -gt 55 ] && echo true || echo false
    false

Tests can be combined with logical AND and OR.

    $ [ 66 -gt 55 -a 66 -lt 500 ] && echo true || echo false
    true
    $ [ 66 -gt 55 -a 660 -lt 500 ] && echo true || echo false
    false
    $ [ 66 -gt 55 -o 660 -lt 500 ] && echo true || echo false
    true

5.2. If then elif

The if then else construction is about choice. If a certain condition is met, then execute something, else execute something else:

    #!/bin/bash
    count=42
    if [ $count -eq 42 ]
    then
        echo "42 is correct."
    elif [ $count -gt 42 ]
    then
        echo "Too much."
    else
        echo "Not enough."
    fi

5.3. For loop

The example below shows the syntax of a classical for loop in bash:

    for i in 1 2 4
    do
        echo $i
    done

The same example as above can be written without the embedded shell using the bash {from..to} shorthand.

    #!/bin/bash
    for counter in {1..20}
    do
        echo counting from 1 to 20, now at $counter
        sleep 1
    done

5.4. While loop

This is the basis structure for a loop while:

    i=100;
    while [ $i -ge 0 ] ;
    do
        echo Counting down, from 100 to 0, now at $i;
        let i--;
    don e

6. Tasks

  1. Write a script that counts the number of files ending in .txt in the current directory
  2. Wrap an if statement around the script so it is also correct when there are zero files ending in .txt

home

Author: Andres Amezquita

Created: 2023-04-16 dom 00:50