1
|
#!/bin/bash
|
2
|
# Sets a password environment variable
|
3
|
|
4
|
self="$(readlink -f -- "$BASH_SOURCE")"
|
5
|
|
6
|
out=/dev/tty
|
7
|
function echo2() { echo "$@" >"$out";}
|
8
|
|
9
|
store=
|
10
|
test _"$1" = _- && { store=1; shift;}
|
11
|
|
12
|
if ! test "$#" -ge 1; then
|
13
|
echo2 "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >"$out"
|
14
|
exit 2
|
15
|
fi
|
16
|
|
17
|
desc="$2"
|
18
|
test -n "$desc" || desc="the $1 password"
|
19
|
|
20
|
function trace()
|
21
|
{
|
22
|
{ for arg in "$@"; do printf "%q " "$arg"; done; echo;} >"$out"
|
23
|
}
|
24
|
|
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"
|
30
|
fi
|
31
|
trace . "$self" - "$@"
|
32
|
|
33
|
test -z "$store" && exit 1 # just direct user how to store password
|
34
|
read -s -p "Enter $desc password: " 2>"$out"; echo2
|
35
|
export "$1"="$REPLY"
|
36
|
fi
|