1
+ #! /usr/bin/env bash
1
2
# A stack, using bash arrays.
2
3
# ---------------------------------------------------------------------------
3
4
# This software is released under a BSD license, adapted from
40
41
# Usage: stack_destroy name
41
42
function stack_destroy
42
43
{
43
- : ${1?' Missing stack name' }
44
+ : " ${1?' Missing stack name' } "
44
45
eval " unset _stack_$1 _stack_$1 _i"
45
46
return 0
46
47
}
@@ -50,10 +51,10 @@ function stack_destroy
50
51
# Usage: stack_push stack item ...
51
52
function stack_push
52
53
{
53
- : ${1?' Missing stack name' }
54
- : ${2?' Missing item(s) to push' }
54
+ : " ${1?' Missing stack name' } "
55
+ : " ${2?' Missing item(s) to push' } "
55
56
56
- if no_such_stack $1
57
+ if no_such_stack " $1 "
57
58
then
58
59
echo " No such stack -- $1 " >&2
59
60
return 1
@@ -84,9 +85,9 @@ function stack_push
84
85
# echo "Size is $n"
85
86
function stack_size
86
87
{
87
- : ${1?' Missing stack name' }
88
- : ${2?' Missing name of variable for stack size result' }
89
- if no_such_stack $1
88
+ : " ${1?' Missing stack name' } "
89
+ : " ${2?' Missing name of variable for stack size result' } "
90
+ if no_such_stack " $1 "
90
91
then
91
92
echo " No such stack -- $1 " >&2
92
93
return 1
@@ -96,8 +97,8 @@ function stack_size
96
97
97
98
function no_such_stack
98
99
{
99
- : ${1?' Missing stack name' }
100
- stack_exists $1
100
+ : " ${1?' Missing stack name' } "
101
+ stack_exists " $1 "
101
102
ret=$?
102
103
declare -i x
103
104
let x=" 1-$ret "
@@ -124,12 +125,12 @@ function no_such_stack
124
125
125
126
function stack_pop
126
127
{
127
- : ${1?' Missing stack name' }
128
- : ${2?' Missing name of variable for popped result' }
128
+ : " ${1?' Missing stack name' } "
129
+ : " ${2?' Missing name of variable for popped result' } "
129
130
130
131
eval ' let _i=$' " _stack_$1 _i"
131
132
132
- if no_such_stack $1
133
+ if no_such_stack " $1 "
133
134
then
134
135
echo " No such stack -- $1 " >&2
135
136
return 1
@@ -158,24 +159,25 @@ function stack_pop
158
159
159
160
function stack_print
160
161
{
161
- : ${1?' Missing stack name' }
162
+ : " ${1?' Missing stack name' } "
162
163
163
- if no_such_stack $1
164
+ if no_such_stack " $1 "
164
165
then
165
166
echo " No such stack -- $1 " >&2
166
167
return 1
167
168
fi
168
169
169
170
tmp=" "
170
- eval ' let _i=$' _stack_$1 _i
171
+ eval ' let _i=$' " _stack_$1 _i"
171
172
172
- while (( $ _i > 0 ))
173
+ while (( _i > 0 ))
173
174
do
174
- (( _i= ${_i} - 1 )) || true
175
+ (( _i = _i - 1 )) || true
175
176
eval ' e=$' " {_stack_$1 [$_i ]}"
177
+ # shellcheck disable=SC2154
176
178
tmp=" $tmp $e "
177
179
done
178
-
180
+ # shellcheck disable=SC2086
179
181
echo " (" $tmp " )"
180
182
}
181
183
@@ -190,14 +192,14 @@ function stack_print
190
192
191
193
function stack_new
192
194
{
193
- : ${1?' Missing stack name' }
194
- if stack_exists $1
195
+ : " ${1?' Missing stack name' } "
196
+ if stack_exists " $1 "
195
197
then
196
198
echo " Stack already exists -- $1 " >&2
197
199
return 1
198
200
fi
199
201
200
- if [[ ` uname` == " Darwin" ]]; then
202
+ if [[ $( uname) == " Darwin" ]]; then
201
203
eval " declare -ag _stack_$1 " >& /dev/null || true
202
204
eval " declare -ig _stack_$1 _i" >& /dev/null || true
203
205
else
@@ -207,7 +209,7 @@ function stack_new
207
209
208
210
variableName=" _stack_$1 _i"
209
211
variableVal=" 0"
210
- eval ${variableName} = ` echo -ne \" " ${variableVal} " \" `
212
+ eval " ${variableName} " = " $( echo -ne \" " ${variableVal} " \" ) "
211
213
212
214
return 0
213
215
}
@@ -223,7 +225,7 @@ function stack_new
223
225
224
226
function stack_exists
225
227
{
226
- : ${1?' Missing stack name' }
228
+ : " ${1?' Missing stack name' } "
227
229
228
230
eval ' _i=$' " {_stack_$1 _i:-}"
229
231
if [[ -z " $_i " ]]
@@ -246,7 +248,8 @@ function stack_exists
246
248
# echo "Got $top"
247
249
function stack_peek
248
250
{
249
- stack_pop $1 $2
250
- eval argument_name=\$ $2
251
- stack_push $1 $argument_name
251
+ stack_pop " $1 " " $2 "
252
+ eval argument_name=" \$ $2 "
253
+ # shellcheck disable=SC2154
254
+ stack_push " $1 " " $argument_name "
252
255
}
0 commit comments