-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
87 lines (58 loc) · 3.01 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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.