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 ### 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 ```bash
eval "$(from="$0" easyoptions.rb "$@" || echo exit 1)" 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: 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 ```bash
export some_option="yes" some_option="yes"
export some_value="10" some_value="10"
unset arguments unset arguments
arguments+=("foo") arguments+=("foo")
export arguments
``` ```
## Contributing ## Contributing

View File

@ -2,7 +2,7 @@
# Encoding: ISO-8859-1 # Encoding: ISO-8859-1
## ##
## EasyOptions 2014.8.3 ## EasyOptions 2014.9.11
## Copyright (c) 2013, 2014 Renato Silva ## Copyright (c) 2013, 2014 Renato Silva
## GNU GPLv2 licensed ## GNU GPLv2 licensed
## ##
@ -56,8 +56,8 @@
## This script can be used from Bash scripts as well. If the $from environment ## 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 ## 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 ## parse the documentation and the provided options. Then, instead of parsing
## the options into Ruby variables, evaluable export statements will be ## the options into Ruby variables, evaluable assignment statements will be
## generated for corresponding Bash environment variables. For example: ## generated for the corresponding Bash environment variables. For example:
## ##
## eval "$(from="$0" @script.name "$@" || echo exit 1)" ## 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 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: ## and one regular argument abc, then the evaluable output would look like this:
## ##
## export option="yes" ## option="yes"
## export some_value="10" ## some_value="10"
## unset arguments ## unset arguments
## arguments+=("abc") ## arguments+=("abc")
## export arguments ## arguments
## ##
class Option class Option
@ -221,11 +221,10 @@ end
# Bash support # Bash support
if BashOutput then if BashOutput then
$options.keys.each do |name| $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 end
puts "unset arguments" puts "unset arguments"
$arguments.each do |argument| $arguments.each do |argument|
puts "arguments+=(\"#{argument}\")" puts "arguments+=(\"#{argument}\")"
end end
puts "export arguments"
end end

View File

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