~pi/aka

ref: 2d5569bf053ca59b1d502e8e1bea33f4dd76a9db aka/aka -rwxr-xr-x 1003 bytes
2d5569bfPaul Ivanov add related project and doit example 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
if [ $# -eq 0 ] ; then
    set -- -h # print usage on 0 arguments
fi

if [ $# -ge 1 ] ; then
    case $1 in
        -h|--help|-help|-H)
echo "Usage: aka alias               show stored aliases"
echo "       aka alias NAME CMD...   store an alias for CMD"
echo "       aka NAME [...]          run CMD, optionally with more arguments"
            exit 0
            ;;
    esac
fi

aliasFile=${AKA_FILE:-./.aka}
# Is this an alias creation? If so, we should have at least 3 arguments,
# such as:
#
# $ aka alias pager more
#    \     \     \    \
#     $0    $1    $2   $3
#
#
if [ $# -ge 3 ]  && [ $1 = alias ] ; then
    # cmd='pager'
    cmd=$2
    # make 'more' the new $1
    shift 2
    # remove previous aliases for 'pager'
    [ -e "$aliasFile" ] && grep -v "alias $cmd=" "$aliasFile"  > "$aliasFile"~
    echo "alias $cmd='$*'" >> "$aliasFile"~ && mv "$aliasFile"~ "$aliasFile"
    exit 0
fi

# Load up aliases...
[ -e "$aliasFile" ] && . "$aliasFile"

# ...and execute command
eval $*