1
|
#!/bin/bash
|
2
|
# Sets a password environment variable
|
3
|
|
4
|
self="$(readlink -f -- "$BASH_SOURCE")"
|
5
|
|
6
|
force=
|
7
|
test _"$1" = _- && { force=1; shift;}
|
8
|
|
9
|
test "$#" -ge 1 || \
|
10
|
{ echo "Usage: . $self env_var_name [msg] (note initial \".\")" >&2; exit 2;}
|
11
|
|
12
|
msg="$2"
|
13
|
test -n "$msg" || msg="$1"
|
14
|
|
15
|
function trace()
|
16
|
{
|
17
|
{ for arg in "$@"; do printf "%q " "$arg"; done; echo;} >&2
|
18
|
}
|
19
|
|
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"
|
23
|
fi
|
24
|
echo "[7m To $action $msg password, run: [0m" >&2
|
25
|
trace . "$self" - "$@"
|
26
|
|
27
|
read -s -p "Enter $msg password: "; echo
|
28
|
export "$1"="$REPLY"
|
29
|
fi
|