| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Shell::Var::Reader; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
105876
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
12
|
|
|
|
1
|
|
|
|
|
27
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
39
|
|
|
6
|
1
|
|
|
1
|
|
555
|
use File::Slurp qw(read_file); |
|
|
1
|
|
|
|
|
27737
|
|
|
|
1
|
|
|
|
|
687
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Shell::Var::Reader - Runs a sh or bash script and returns the variables that have been set as well as their values. |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.3.2 |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.3.2'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Lets say '/usr/local/etc/someconfig.conf' which is basically a shell |
|
23
|
|
|
|
|
|
|
config and read via include in a sh or bash script, this can be used for |
|
24
|
|
|
|
|
|
|
getting a hash ref conttaining them. |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Similarly on systems like FreeBSD, this is also useful for reading '/etc/rc.conf'. |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Shell::Var::Reader; |
|
29
|
|
|
|
|
|
|
use Data::Dumper; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $found_vars=Shell::Var::Reader->read_in('/usr/local/etc/someconfig.conf'); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
print Dumper($found_vars); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 SUBROUTINES |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 read_in |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
This runs a specified file as a include and then figures out what the |
|
40
|
|
|
|
|
|
|
new variables are. fetching them. |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub read_in { |
|
45
|
0
|
|
|
0
|
1
|
|
my $file = $_[1]; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if ( !defined($file) ) { |
|
48
|
0
|
|
|
|
|
|
die('No file specified'); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if ( !-f $file ) { |
|
52
|
0
|
|
|
|
|
|
die( '"' . $file . '" does not exist or is not a file' ); |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# figure out if we are using bash or not |
|
56
|
0
|
0
|
|
|
|
|
my $raw_file = read_file($file) or die 'Failed to read "' . $file . '"'; |
|
57
|
0
|
|
|
|
|
|
my @raw_split = split( /\n/, $raw_file ); |
|
58
|
0
|
|
|
|
|
|
my $shell = 'sh'; |
|
59
|
0
|
0
|
0
|
|
|
|
if ( defined( $raw_split[0] ) && ( $raw_split[0] =~ /^\#\!.*bash/ ) ) { |
|
60
|
0
|
|
|
|
|
|
$shell = 'bash'; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# |
|
64
|
|
|
|
|
|
|
# figure out what variables already exist... |
|
65
|
|
|
|
|
|
|
# |
|
66
|
0
|
|
|
|
|
|
my $cmd = $shell . " -c 'if [ -z \"\$BASH_VERSION\" ]; then set; else set -o posix; set; fi'"; |
|
67
|
0
|
|
|
|
|
|
my $results = `$cmd`; |
|
68
|
0
|
|
|
|
|
|
my $base_vars = {'ShellVarReaderFile'=>1}; |
|
69
|
0
|
|
|
|
|
|
my @results_split = split( /\n/, $results ); |
|
70
|
0
|
|
|
|
|
|
foreach my $line (@results_split) { |
|
71
|
0
|
0
|
|
|
|
|
if ( $line =~ /^[\_a-zA-Z]+[\_a-zA-Z0-9]*\=/ ) { |
|
72
|
0
|
|
|
|
|
|
my @line_split = split( /=/, $line, 2 ); |
|
73
|
0
|
|
|
|
|
|
$base_vars->{ $line_split[0] } = 1; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# |
|
78
|
|
|
|
|
|
|
# Figure out what has been set |
|
79
|
|
|
|
|
|
|
# |
|
80
|
0
|
|
|
|
|
|
$ENV{ShellVarReaderFile} = $file; |
|
81
|
0
|
|
|
|
|
|
$cmd |
|
82
|
|
|
|
|
|
|
= $shell . " -c ' . \"\$ShellVarReaderFile\" > /dev/null 2> /dev/null ; if [ -z \"\$BASH_VERSION\" ]; then set; else set -o posix; set; fi'"; |
|
83
|
0
|
|
|
|
|
|
$results = `$cmd`; |
|
84
|
0
|
|
|
|
|
|
my $found_vars = {}; |
|
85
|
0
|
|
|
|
|
|
@results_split = split( /\n/, $results ); |
|
86
|
0
|
|
|
|
|
|
my $multiline = 0; |
|
87
|
0
|
|
|
|
|
|
my $var_key; |
|
88
|
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
foreach my $line (@results_split) { |
|
90
|
0
|
0
|
0
|
|
|
|
if ( $line =~ /^[\_a-zA-Z]+[\_a-zA-Z0-9]*\=/ && !$multiline ) { |
|
91
|
0
|
|
|
|
|
|
my @line_split = split( /=/, $line, 2 ); |
|
92
|
0
|
|
|
|
|
|
$var_key = $line_split[0]; |
|
93
|
0
|
|
|
|
|
|
$found_vars->{$var_key} = $line_split[1]; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# if the value starts with a ' and does not end with a ' |
|
96
|
|
|
|
|
|
|
# it is going to be multiline |
|
97
|
0
|
0
|
0
|
|
|
|
if ( $found_vars->{$var_key} =~ /^\'/ |
|
98
|
|
|
|
|
|
|
&& $found_vars->{$var_key} !~ /\'$/ ) |
|
99
|
|
|
|
|
|
|
{ |
|
100
|
0
|
|
|
|
|
|
$found_vars->{$var_key} =~ s/^\'//; |
|
101
|
0
|
|
|
|
|
|
$multiline = 1; |
|
102
|
|
|
|
|
|
|
}else { |
|
103
|
0
|
|
|
|
|
|
$found_vars->{$var_key} =~ s/^\'//; |
|
104
|
0
|
|
|
|
|
|
$found_vars->{$var_key} =~ s/\'$//; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
else { |
|
108
|
0
|
|
|
|
|
|
$found_vars->{$var_key} = $line; |
|
109
|
|
|
|
|
|
|
# if it ends with ', then we have reached the end of the variable |
|
110
|
0
|
0
|
|
|
|
|
if ( $found_vars->{$var_key} =~ /\'$/ ) { |
|
111
|
0
|
|
|
|
|
|
$found_vars->{$var_key} =~ s/\'$//; |
|
112
|
0
|
|
|
|
|
|
$multiline = 0; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# |
|
118
|
|
|
|
|
|
|
# remove base vars |
|
119
|
|
|
|
|
|
|
# |
|
120
|
0
|
|
|
|
|
|
my @found_keys=keys(%{ $found_vars }); |
|
|
0
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
foreach my $var_key (@found_keys) { |
|
122
|
0
|
0
|
|
|
|
|
if (defined($base_vars->{$var_key})) { |
|
123
|
0
|
|
|
|
|
|
delete $found_vars->{$var_key}; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
return $found_vars; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Zane C. Bowers-Hadley, C<< >> |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 BUGS |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
137
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
138
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 SUPPORT |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
perldoc Shell::Var::Reader |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
You can also look for information at: |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * Search CPAN |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
L |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This software is Copyright (c) 2022 by Zane C. Bowers-Hadley. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This is free software, licensed under: |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; # End of Shell::Var::Reader |