Skip to content

Script to check Bash scripts for empty or undefined Bash variables

License

Notifications You must be signed in to change notification settings

urbanware-org/varbash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

README (VarBash)

    Definition

        Script to check Bash scripts for empty or undefined Bash variables to
        avoid some possible mess.

    Details

        Empty or undefined variables inside scripts are unnecessary and can
        lead to some (maybe fatal) problems. Even though, a developer of shell
        scripts should survey the code before using them, typos in variables
        may be missed.

        This is a simple example:

            #!/usr/bin/env bash

            remove_dir="$1"
            rm -fR ${remvoe_dir}/*

        In this case, there is a typo inside the variable name in the line
        that contains the 'rm' command. Due to this, that variable is empty
        and the actual command looks like this:

            rm -fR /*

        When executed, the 'rm' command will remove all directories and files
        from the root directory of the system, leading to severe as well as
        irreversible consequences, no matter which argument gets passed to
        the script. 

        To avoid such a situation, the 'varbash.sh' script tries to detect the
        lines where variables are used that never got assigned before as shown
        in the example above. In this case:

            Line 4:  Possibly undefined variable:  $remvoe_dir/*
            Line 4:  Variable '$remvoe_dir/*' in same line with 'rm' command

        However, it is not absolutely reliable and returns still some false
        positives.

        Feel free to modify!

    Usage

        Just run the script and pass the path to the Bash script you want to
        check as the first command-line argument, for example:

            $ ./varbash.sh /tmp/foobar.sh

        If you want to check multiple files (e.g. if they have a releation),
        you can simply merge them. For example, to check all script files
        inside the directory '/opt/scripts' and its sub-directories with the
        file extension '.sh', you can do that as follows:

            $ cat $(find /opt/scripts | grep "\.sh$") >> /tmp/scripts.tmp
            $ ./varbash.sh /tmp/scripts.tmp

    Exit codes

        If you want to redirect or suppress the standard output, you can
        evaluate the exit codes as follows. Notice that these have been
        revised.

        Version 1.0.3 and above:

            0 - No error and no variable issues have been found
            1 - Wrong number of command-line arguments
            2 - The given path is no file
            3 - The given file does not exist
            4 - All types of variable issues have been found
            5 - Variables without initially assigned value found, only
            6 - Undefined variables found, only

        Below version 1.0.3:

            0 - No error and no variable issues have been found
            1 - The given path is either no file or does not exist
            2 - No error but variable issues have been found

    Contact

        Any suggestions, questions, bugs to report or feedback to give? If so,
        you can find the contact information inside the 'CONTACT' file.