Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/alias.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2011 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
alias foo='print hello'
35
if [[ $(foo) != hello ]]
36
then err_exit 'foo, where foo is alias for "print hello" failed'
37
fi
38
if [[ $(foo world) != 'hello world' ]]
39
then err_exit 'foo world, where foo is alias for "print hello" failed'
40
fi
41
alias foo='print hello '
42
alias bar=world
43
if [[ $(foo bar) != 'hello world' ]]
44
then err_exit 'foo bar, where foo is alias for "print hello " failed'
45
fi
46
if [[ $(foo \bar) != 'hello bar' ]]
47
then err_exit 'foo \bar, where foo is alias for "print hello " failed'
48
fi
49
alias bar='foo world'
50
if [[ $(bar) != 'hello world' ]]
51
then err_exit 'bar, where bar is alias for "foo world" failed'
52
fi
53
if [[ $(alias bar) != "bar='foo world'" ]]
54
then err_exit 'alias bar, where bar is alias for "foo world" failed'
55
fi
56
unalias foo || err_exit "unalias foo failed"
57
alias foo 2> /dev/null && err_exit "alias for non-existent alias foo returns true"
58
unset bar
59
alias bar="print foo$bar"
60
bar=bar
61
if [[ $(bar) != foo ]]
62
then err_exit 'alias bar, where bar is alias for "print foo$bar" failed'
63
fi
64
unset bar
65
alias bar='print hello'
66
if [[ $bar != '' ]]
67
then err_exit 'alias bar cause variable bar to be set'
68
fi
69
alias !!=print
70
if [[ $(!! hello 2>/dev/null) != hello ]]
71
then err_exit 'alias for !!=print not working'
72
fi
73
alias foo=echo
74
if [[ $(print "$(foo bar)" ) != bar ]]
75
then err_exit 'alias in command substitution not working'
76
fi
77
( unalias foo)
78
if [[ $(foo bar 2> /dev/null) != bar ]]
79
then err_exit 'alias not working after unalias in subshell'
80
fi
81
builtin -d rm 2> /dev/null
82
if whence rm > /dev/null
83
then [[ ! $(alias -t | grep rm= ) ]] && err_exit 'tracked alias not set'
84
PATH=$PATH
85
[[ $(alias -t | grep rm= ) ]] && err_exit 'tracked alias not cleared'
86
fi
87
if hash -r 2>/dev/null && [[ ! $(hash) ]]
88
then PATH=$tmp:/bin:/usr/bin
89
for i in foo -foo --
90
do print ':' > $tmp/$i
91
chmod +x $tmp/$i
92
hash -r -- $i 2>/dev/null || err_exit "hash -r -- $i failed"
93
[[ $(hash) == $i=$tmp/$i ]] || err_exit "hash -r -- $i failed, expected $i=$tmp/$i, got $(hash)"
94
done
95
else err_exit 'hash -r failed'
96
fi
97
( alias :pr=print) 2> /dev/null || err_exit 'alias beginning with : fails'
98
( alias p:r=print) 2> /dev/null || err_exit 'alias with : in name fails'
99
100
unalias no_such_alias && err_exit 'unalias should return non-zero for unknown alias'
101
102
exit $((Errors<125?Errors:125))
103
104