Project

General

Profile

« Previous | Next » 

Revision 1144

env_password: Fixed to set the environment variable in the calling shell. Do this by cc-ing the tty only on messages before the "Enter password" prompt, because the redirect creates a subshell which causes the env var to only be set within that subshell.

View differences:

env_password
5 5

  
6 6
set -o pipefail
7 7

  
8
test -t 2 && ccTty= || ccTty=1 # cc the tty if stderr is a log file
9
{
10 8
store=
11
test _"$1" = _- && { store=1; shift;}
9
test "$1" = - && { store=1; shift;}
12 10

  
13 11
if ! test "$#" -ge 1; then
14
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s
12
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >&2
15 13
    exit 2
16 14
fi
17 15

  
18 16
desc="$2"
19 17
test -n "$desc" || desc="the $1 password"
20 18

  
21
function trace()
22
{
23
    { for arg in "$@"; do printf "%q " "$arg"; done; echo;}
24
}
19
function trace() { for arg in "$@"; do printf "%q " "$arg"; done; echo;}
25 20

  
21
function traceSelf() { trace . "$self" - "$@";}
22

  
26 23
if test -n "$store" -o -z "${!1+t}"; then # env var with name $1 is unset
27 24
    if test -n "$store"; then
28
        echo " To change the saved value for $desc, run: "
25
        echo " To change the saved value for $desc, run: " >&2
26
        traceSelf "$@" >&2
29 27
    else
28
        test -t 2 && ccTty= || ccTty=1 # cc the tty if stderr is a log file
29
        {
30 30
        echo " You must first store $desc. At the shell, run: "
31
        traceSelf "$@"
32
        }|tee ${ccTty:+/dev/tty} >&2
31 33
    fi
32
    trace . "$self" - "$@"
33 34
    
34 35
    test -z "$store" && exit 1 # just direct user how to store password
35 36
    read -s -p "Enter $desc: "; echo
36 37
    export "$1"="$REPLY"
37 38
fi
38
} 2>&1|tee ${ccTty:+/dev/tty} >&2 || exit

Also available in: Unified diff