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
test "${BASH_LINENO[0]}" = 0 && missing_dot=true || missing_dot=false
9
    # whether run without initial "."
10

    
11
store=
12
test "$1" = - && { store=1; shift;}
13

    
14
if $missing_dot || ! test "$#" -ge 1; then
15
    $missing_dot && self="$0"
16
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >&2
17
    exit 2
18
fi
19

    
20
desc="$2"
21
test -n "$desc" || desc="the $1 password"
22

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

    
25
function traceSelf() { trace . "$self" - "$@";}
26

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