Project

General

Profile

1
#!/bin/bash
2
# Sets a password environment variable
3

    
4
self="$(readlink -f -- "$BASH_SOURCE")"
5

    
6
set -o pipefail
7

    
8
store=
9
test "$1" = - && { store=1; shift;}
10

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

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

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

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

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