Avoid variable exporting for Bash scripts.

Exporting should not be required and it may lead to security issues, for example access of a --password option by other applications.
This commit is contained in:
Renato Silva 2014-09-11 00:48:49 -03:00
parent 3faceb97dd
commit 3d8b0c3e15
3 changed files with 15 additions and 18 deletions

View File

@ -86,7 +86,7 @@ options=(o=option some-boolean some-value=?)
### Ruby version in Bash
The Ruby version can be used from Bash scripts as well since it is faster. If the `$from` environment variable is set, that will be assumed as the source Bash script from which to parse the documentation and the provided options. Then, instead of parsing the options into Ruby variables, evaluable export statements will be generated for corresponding Bash environment variables. Instead of sourcing the Bash script we call the Ruby version, for example:
The Ruby version can be used from Bash scripts as well since it is faster. If the `$from` environment variable is set, that will be assumed as the source Bash script from which to parse the documentation and the provided options. Then, instead of parsing the options into Ruby variables, evaluable assignment statements will be generated for corresponding Bash environment variables. Instead of sourcing the Bash script we call the Ruby version, for example:
```bash
eval "$(from="$0" easyoptions.rb "$@" || echo exit 1)"
@ -95,11 +95,10 @@ eval "$(from="$0" easyoptions.rb "$@" || echo exit 1)"
If the script containing this command is documented as in the example above, and it is executed from command line with the `-o` and `--some-value=10` options, and one regular argument `foo`, then the evaluable output would look like this:
```bash
export some_option="yes"
export some_value="10"
some_option="yes"
some_value="10"
unset arguments
arguments+=("foo")
export arguments
```
## Contributing

View File

@ -2,7 +2,7 @@
# Encoding: ISO-8859-1
##
## EasyOptions 2014.8.3
## EasyOptions 2014.9.11
## Copyright (c) 2013, 2014 Renato Silva
## GNU GPLv2 licensed
##
@ -56,8 +56,8 @@
## This script can be used from Bash scripts as well. If the $from environment
## variable is set, that will be assumed as the source Bash script from which to
## parse the documentation and the provided options. Then, instead of parsing
## the options into Ruby variables, evaluable export statements will be
## generated for corresponding Bash environment variables. For example:
## the options into Ruby variables, evaluable assignment statements will be
## generated for the corresponding Bash environment variables. For example:
##
## eval "$(from="$0" @script.name "$@" || echo exit 1)"
##
@ -65,11 +65,11 @@
## and it is executed from command line with the -o and --some-value=10 options,
## and one regular argument abc, then the evaluable output would look like this:
##
## export option="yes"
## export some_value="10"
## option="yes"
## some_value="10"
## unset arguments
## arguments+=("abc")
## export arguments
## arguments
##
class Option
@ -221,11 +221,10 @@ end
# Bash support
if BashOutput then
$options.keys.each do |name|
puts "export #{name}=\"#{$options[name].to_s.sub("true", "yes")}\""
puts "#{name}=\"#{$options[name].to_s.sub("true", "yes")}\""
end
puts "unset arguments"
$arguments.each do |argument|
puts "arguments+=(\"#{argument}\")"
end
puts "export arguments"
end

View File

@ -1,7 +1,7 @@
#!/bin/bash
##
## EasyOptions 2014.7.9
## EasyOptions 2014.9.11
## Copyright (c) 2013 Renato Silva
## GNU GPLv2 licensed
##
@ -129,14 +129,14 @@ parse_options() {
if [[ "$option" = "$known_option_name" ]]; then
option_value="yes"
known_option_var=$(echo "$known_option_var" | tr "-" "_")
eval "export $known_option_var=\"$option_value\""
eval "$known_option_var=\"$option_value\""
break
# Long option
elif [[ "$option" = -$known_option_name && "$known_option_var" != "?" ]]; then
option_value="yes"
known_option_var=$(echo "$known_option_var" | tr "-" "_")
eval "export $known_option_var=\"$option_value\""
eval "$known_option_var=\"$option_value\""
break
# Long option with value in next parameter
@ -148,14 +148,14 @@ parse_options() {
fi
OPTIND=$((OPTIND + 1))
known_option_var=$(echo "$known_option_name" | tr "-" "_")
eval "export $known_option_var=\"$option_value\""
eval "$known_option_var=\"$option_value\""
break
# Long option with value after equal sign
elif [[ "$option" = -$known_option_name=* && "$known_option_var" = "?" ]]; then
option_value=${option#*=}
known_option_var=$(echo "$known_option_name" | tr "-" "_")
eval "export $known_option_var=\"$option_value\""
eval "$known_option_var=\"$option_value\""
break
# Long option with unnecessary value
@ -199,7 +199,6 @@ parse_options() {
fi
shift
done
export arguments options
}
parse_options "$@"