Project

General

Profile

« Previous | Next » 

Revision 1952

Scripts that are meant to be run in the calling shell: Fixed bug where running the script as a program (without initial ".") wouldn't be able to call return in something that was not a function. Converted all code to a <script_name>_main method so that return would work properly again. Converted all variables to local variables.

View differences:

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

  
4
self="$(readlink -f -- "${BASH_SOURCE[0]}")"
5

  
6
set -o pipefail
7

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

  
11
# Was run without initial ".", or with insufficient parameters
12
if test "${BASH_LINENO[0]}" = 0 -o "$#" -lt 1; then
13
    echo "Usage: . $self env_var [desc] (note initial \".\")"|fold -s >&2
14
    return 2
15
fi
16

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

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

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

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

Also available in: Unified diff