Project

General

Profile

1 183 aaronmk
#!/bin/bash
2 131 aaronmk
# Sets a password environment variable
3
4 1950 aaronmk
self="$(readlink -f -- "${BASH_SOURCE[0]}")"
5 291 aaronmk
6 1093 aaronmk
set -o pipefail
7
8 1090 aaronmk
store=
9 1144 aaronmk
test "$1" = - && { store=1; shift;}
10 131 aaronmk
11 1950 aaronmk
# Was run without initial ".", or with insufficient parameters
12
if test "${BASH_LINENO[0]}" = 0 -o "$#" -lt 1; then
13 1144 aaronmk
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >&2
14 1950 aaronmk
    return 2
15 1090 aaronmk
fi
16 251 aaronmk
17 1090 aaronmk
desc="$2"
18
test -n "$desc" || desc="the $1 password"
19
20 1144 aaronmk
function trace() { for arg in "$@"; do printf "%q " "$arg"; done; echo;}
21 290 aaronmk
22 1144 aaronmk
function traceSelf() { trace . "$self" - "$@";}
23
24 1090 aaronmk
if test -n "$store" -o -z "${!1+t}"; then # env var with name $1 is unset
25
    if test -n "$store"; then
26 1144 aaronmk
        echo " To change the saved value for $desc, run: " >&2
27
        traceSelf "$@" >&2
28 1090 aaronmk
    else
29 1144 aaronmk
        test -t 2 && ccTty= || ccTty=1 # cc the tty if stderr is a log file
30
        {
31 1091 aaronmk
        echo " You must first store $desc. At the shell, run: "
32 1144 aaronmk
        traceSelf "$@"
33
        }|tee ${ccTty:+/dev/tty} >&2
34 292 aaronmk
    fi
35 293 aaronmk
36 1951 aaronmk
    test -z "$store" && return 1 # just direct user how to store password
37 1091 aaronmk
    read -s -p "Enter $desc: "; echo
38 131 aaronmk
    export "$1"="$REPLY"
39
fi