1+ #! /usr/bin/env bash
12# A stack, using bash arrays.
23# ---------------------------------------------------------------------------
34# This software is released under a BSD license, adapted from
4041# Usage: stack_destroy name
4142function stack_destroy
4243{
43- : ${1?' Missing stack name' }
44+ : " ${1?' Missing stack name' } "
4445 eval " unset _stack_$1 _stack_$1 _i"
4546 return 0
4647}
@@ -50,10 +51,10 @@ function stack_destroy
5051# Usage: stack_push stack item ...
5152function stack_push
5253{
53- : ${1?' Missing stack name' }
54- : ${2?' Missing item(s) to push' }
54+ : " ${1?' Missing stack name' } "
55+ : " ${2?' Missing item(s) to push' } "
5556
56- if no_such_stack $1
57+ if no_such_stack " $1 "
5758 then
5859 echo " No such stack -- $1 " >&2
5960 return 1
@@ -84,9 +85,9 @@ function stack_push
8485# echo "Size is $n"
8586function stack_size
8687{
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 "
9091 then
9192 echo " No such stack -- $1 " >&2
9293 return 1
@@ -96,8 +97,8 @@ function stack_size
9697
9798function no_such_stack
9899{
99- : ${1?' Missing stack name' }
100- stack_exists $1
100+ : " ${1?' Missing stack name' } "
101+ stack_exists " $1 "
101102 ret=$?
102103 declare -i x
103104 let x=" 1-$ret "
@@ -124,12 +125,12 @@ function no_such_stack
124125
125126function stack_pop
126127{
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' } "
129130
130131 eval ' let _i=$' " _stack_$1 _i"
131132
132- if no_such_stack $1
133+ if no_such_stack " $1 "
133134 then
134135 echo " No such stack -- $1 " >&2
135136 return 1
@@ -158,24 +159,25 @@ function stack_pop
158159
159160function stack_print
160161{
161- : ${1?' Missing stack name' }
162+ : " ${1?' Missing stack name' } "
162163
163- if no_such_stack $1
164+ if no_such_stack " $1 "
164165 then
165166 echo " No such stack -- $1 " >&2
166167 return 1
167168 fi
168169
169170 tmp=" "
170- eval ' let _i=$' _stack_$1 _i
171+ eval ' let _i=$' " _stack_$1 _i"
171172
172- while (( $ _i > 0 ))
173+ while (( _i > 0 ))
173174 do
174- (( _i= ${_i} - 1 )) || true
175+ (( _i = _i - 1 )) || true
175176 eval ' e=$' " {_stack_$1 [$_i ]}"
177+ # shellcheck disable=SC2154
176178 tmp=" $tmp $e "
177179 done
178-
180+ # shellcheck disable=SC2086
179181 echo " (" $tmp " )"
180182}
181183
@@ -190,14 +192,14 @@ function stack_print
190192
191193function stack_new
192194{
193- : ${1?' Missing stack name' }
194- if stack_exists $1
195+ : " ${1?' Missing stack name' } "
196+ if stack_exists " $1 "
195197 then
196198 echo " Stack already exists -- $1 " >&2
197199 return 1
198200 fi
199201
200- if [[ ` uname` == " Darwin" ]]; then
202+ if [[ $( uname) == " Darwin" ]]; then
201203 eval " declare -ag _stack_$1 " >& /dev/null || true
202204 eval " declare -ig _stack_$1 _i" >& /dev/null || true
203205 else
@@ -207,7 +209,7 @@ function stack_new
207209
208210 variableName=" _stack_$1 _i"
209211 variableVal=" 0"
210- eval ${variableName} = ` echo -ne \" " ${variableVal} " \" `
212+ eval " ${variableName} " = " $( echo -ne \" " ${variableVal} " \" ) "
211213
212214 return 0
213215}
@@ -223,7 +225,7 @@ function stack_new
223225
224226function stack_exists
225227{
226- : ${1?' Missing stack name' }
228+ : " ${1?' Missing stack name' } "
227229
228230 eval ' _i=$' " {_stack_$1 _i:-}"
229231 if [[ -z " $_i " ]]
@@ -246,7 +248,8 @@ function stack_exists
246248# echo "Got $top"
247249function stack_peek
248250{
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 "
252255}
0 commit comments