From 3d8b0c3e15ff746a8fb48ceae0282b4bcd42998c Mon Sep 17 00:00:00 2001 From: Renato Silva Date: Thu, 11 Sep 2014 00:48:49 -0300 Subject: [PATCH] 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. --- README.md | 7 +++---- easyoptions.rb | 15 +++++++-------- easyoptions.sh | 11 +++++------ 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 81dd34d..238d558 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/easyoptions.rb b/easyoptions.rb index af50e88..c15ef82 100644 --- a/easyoptions.rb +++ b/easyoptions.rb @@ -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 diff --git a/easyoptions.sh b/easyoptions.sh index 373a922..3cdea9e 100644 --- a/easyoptions.sh +++ b/easyoptions.sh @@ -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 "$@"