#Modified code #!/bin/sh
Modified code
#!/bin/sh
dump_arguments()
# string function - prints arguments (position and content)
# indicates empty arguments and integer numbers
{
punct=${1+:}
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected${punct:-.}"
i=1
for v
do
if [ -z "$v" ]
then type=' (empty)'
elif [ "$v" -eq "$v" ] 2>/dev/null
then type=' (integer)'
else type=
fi
printf "[%d]: '%s'%s\n" \
"$i" "$v" "$type"
i=$((i+1))
done
}
#Modified code #!/bin/sh
dump_arguments()
# string function - prints arguments (position and content)
# indicates empty arguments and integer numbers
{
punct=${1+:}
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected${punct:-.}"
i=1
for v
do
if [ -z "$v" ]
then type=' (empty)'
elif [ "$v" -eq "$v" ] 2>/dev/null
then type=' (integer)'
else type=
fi
printf "[%d]: '%s'%s\n" \
"$i" "$v" "$type"
i=$((i+1))
done
}
Modified code
#!/bin/sh
dump_arguments()
# string function - prints arguments (position and content)
# indicates empty arguments and integer numbers
{
punct=${1+:}
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected${punct:-.}"
i=1
for v
do
if [ -z "$v" ]
then type=' (empty)'
elif [ "$v" -eq "$v" ] 2>/dev/null
then type=' (integer)'
else type=
fi
printf "[%d]: '%s'%s\n" \
"$i" "$v" "$type"
i=$((i+1))
done
}
- 87.9k
- 14
- 104
- 325
Is it intentional that strings such as ' 5'
(with leading and/or trailing spaces) are counted as integers?
That said, I think it's more natural to iterate over arguments using a for
loop instead.
That's surprising, as the output isn't an error, but expected when calling the function. It should be up to the caller to choose where stream 1 goes.
#Modified code #!/bin/sh
dump_arguments()
# string function - prints arguments (position and content)
# indicates empty arguments and integer numbers
{
punct=${1+:}
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected${punct:-.}"
i=1
for v
do
if [ -z "$v" ]
then type=' (empty)'
elif [ "$v" -eq "$v" ] 2>/dev/null
then type=' (integer)'
else type=
fi
printf "[%d]: '%s'%s\n" \
"$i" "$v" "$type"
i=$((i+1))
done
}
That's surprising, as the output isn't an error, but expected when calling the function. It should be up to the caller to choose where stream 1 goes.
Is it intentional that strings such as ' 5'
(with leading and/or trailing spaces) are counted as integers?
That said, I think it's more natural to iterate over arguments using a for
loop instead.
That's surprising, as the output isn't an error, but expected when calling the function. It should be up to the caller to choose where stream 1 goes.
#Modified code #!/bin/sh
dump_arguments()
# string function - prints arguments (position and content)
# indicates empty arguments and integer numbers
{
punct=${1+:}
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected${punct:-.}"
i=1
for v
do
if [ -z "$v" ]
then type=' (empty)'
elif [ "$v" -eq "$v" ] 2>/dev/null
then type=' (integer)'
else type=
fi
printf "[%d]: '%s'%s\n" \
"$i" "$v" "$type"
i=$((i+1))
done
}
printf '%b' "dump_arguments()\\n----------------\\n$# arguments are being inspected"
We could make that easier to read by making each line a separate argument, and ending the line properly (rather than requiring a separate command):
punct=.
${1+false} true || punct=:
printf '%s\n' \
'dump_arguments()' \
'----------------' \
"$# arguments are being inspected$punct"
It seems odd to use %s
here:
printf '%s' "[$i]: '1ドル'"
Why not use the format string more clearly? And this allows us to modify to use %q
if we have a suitable printf:
printf "[%d]: '%s'" "$i" "1ドル"
Perhaps even combine the type into a single print:
type=
[ -z "1ドル" ] && type=' (empty)'
[ "1ドル" -eq "1ドル" ] 2>/dev/null && type=' (integer)'
printf "[%d]: '%s'%s\n" "$i" "1ドル" "$type"
shift 1
Normally written simply as
shift
} >&2
That's surprising, as the output isn't an error, but expected when calling the function. It should be up to the caller to choose where stream 1 goes.