Re: bash scripting question
On 2007-05-17, Bob McGowan <bob_mcgowan@symantec.com> wrote:
>
> Some general comments, mostly aimed at making your code cleaner without
> changing what it does.
>
> First, both 'echo' and 'printf' put their results on standard out. Your
> call of 'printf' is inside command substitution, so its STDOUT becomes
> the command line for 'echo' which just prints to its STDOUT. Why the
> double print out? Just do:
>
> lab_let=$(printf "\\x$(echo $lab_num)")
>
> Next, the 'echo $lab_num' is not needed, $lab_num can stand alone:
>
> lab_let=$(printf "\\x$lab_num")
>
> And, the double quotes escape things, too, so the double backslash is
> not needed:
>
> lab_let=$(printf "\x$lab_num")
>
Thank you for this! I started out with something a little more
complicated, without the variable, trying to insert the hex character
directly into another command. And my testing required that I use a
form that would print something to the command line. I got very worked
up trying to sort out the syntax, and obviously over-did it.
> Then, the line where you increment lab_num can also be simpler. In bash
> the $((...)) alone on a line will replace itself with the result
> (command substitution, again). But, leave off the leading $ sign, and
> it just does the increment:
>
> ((lab_num++))
Oh, great, thanks. I added the echo to stop getting the complaint
about unknown command, but this is better.
>
> So, cut and pasted from a bash shell:
>
> $ lab_num=41
> $ lab_let=$(printf "\x$lab_num")
> $ echo $lab_let
> A
> $ ((lab_num++))
> $ lab_let=$(printf "\x$lab_num")
> $ echo $lab_let
> B
Much improved!
>
> This would need two loops, the outer to increment the 'tens' digit, the
> inner to increment the 'ones' digit, but it would do the trick. For
> example:
>
> x=(0 1 2 3 4 5 6 7 8 9 A B C D E F)
>
I knew there was an array form in bash, but I couldn't find it. I'm
working from the O'Reilly book classic shell scripting, and the only
reference to arrays is in relation to awk scripts. This is a big help.
Thanks alot!
Tyler
Reply to: