2001-07-10 14:25:50 +00:00
|
|
|
#!/bin/bash
|
2004-02-16 17:21:02 +00:00
|
|
|
# ref-params.sh: Dereferencing a parameter passed to a function.
|
|
|
|
# (Complex Example)
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
ITERATIONS=3 # How many times to get input.
|
|
|
|
icount=1
|
|
|
|
|
|
|
|
my_read () {
|
2004-02-16 17:21:02 +00:00
|
|
|
# Called with my_read varname,
|
|
|
|
#+ outputs the previous value between brackets as the default value,
|
|
|
|
#+ then asks for a new value.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
local local_var
|
|
|
|
|
|
|
|
echo -n "Enter a value "
|
2004-02-16 17:21:02 +00:00
|
|
|
eval 'echo -n "[$'$1'] "' # Previous value.
|
|
|
|
# eval echo -n "[\$$1] " # Easier to understand,
|
|
|
|
#+ but loses trailing space in user prompt.
|
2001-07-10 14:25:50 +00:00
|
|
|
read local_var
|
2014-07-05 13:38:40 +00:00
|
|
|
[ -n "$local_var" ] && eval $1=\$local_var
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
# "And-list": if "local_var" then set "$1" to its value.
|
|
|
|
}
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
while [ "$icount" -le "$ITERATIONS" ]
|
|
|
|
do
|
|
|
|
my_read var
|
|
|
|
echo "Entry #$icount = $var"
|
|
|
|
let "icount += 1"
|
|
|
|
echo
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# Thanks to Stephane Chazelas for providing this instructive example.
|
|
|
|
|
|
|
|
exit 0
|