Revision 1090
Added by Aaron Marcuse-Kubitza almost 13 years ago
env_password | ||
---|---|---|
3 | 3 |
|
4 | 4 |
self="$(readlink -f -- "$BASH_SOURCE")" |
5 | 5 |
|
6 |
force=
|
|
7 |
test _"$1" = _- && { force=1; shift;}
|
|
6 |
out=/dev/tty
|
|
7 |
function echo2() { echo "$@" >"$out";}
|
|
8 | 8 |
|
9 |
test "$#" -ge 1 || \
|
|
10 |
{ echo "Usage: . $self env_var_name [msg] (note initial \".\")" >&2; exit 2;}
|
|
9 |
store=
|
|
10 |
test _"$1" = _- && { store=1; shift;}
|
|
11 | 11 |
|
12 |
msg="$2" |
|
13 |
test -n "$msg" || msg="$1" |
|
12 |
if ! test "$#" -ge 1; then |
|
13 |
echo2 "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >"$out" |
|
14 |
exit 2 |
|
15 |
fi |
|
14 | 16 |
|
17 |
desc="$2" |
|
18 |
test -n "$desc" || desc="the $1 password" |
|
19 |
|
|
15 | 20 |
function trace() |
16 | 21 |
{ |
17 |
{ for arg in "$@"; do printf "%q " "$arg"; done; echo;} >&2
|
|
22 |
{ for arg in "$@"; do printf "%q " "$arg"; done; echo;} >"$out"
|
|
18 | 23 |
} |
19 | 24 |
|
20 |
if test -n "$force" -o -z "${!1+t}"; then # env var with name $1 is unset |
|
21 |
if test -n "$force"; then action="change the saved value for" |
|
22 |
else action="avoid having to reenter" |
|
25 |
if test -n "$store" -o -z "${!1+t}"; then # env var with name $1 is unset |
|
26 |
if test -n "$store"; then |
|
27 |
echo2 "[7m To change the saved value for $desc, run: [0m" >"$out" |
|
28 |
else |
|
29 |
echo2 "[101;97m You must first store $desc. At the shell, run: [39;49m" |
|
23 | 30 |
fi |
24 |
echo "[7m To $action $msg password, run: [0m" >&2 |
|
25 | 31 |
trace . "$self" - "$@" |
26 | 32 |
|
27 |
read -s -p "Enter $msg password: "; echo |
|
33 |
test -z "$store" && exit 1 # just direct user how to store password |
|
34 |
read -s -p "Enter $desc password: " 2>"$out"; echo2 |
|
28 | 35 |
export "$1"="$REPLY" |
29 | 36 |
fi |
Also available in: Unified diff
env_password: Print all messages to /dev/tty so the user sees them even if stderr is redirected to a log file. Exit if password not already set, because e.g. scripts run in the background will not be able to prompt for it.