Tyler Smith wrote:
Hi,

I've got a question about a short bash script I wrote. I need it to
--snipped--

#!/bin/bash

lab_num=41

for map_name in aest_90 bush_90 carol_90 comp_90 \
hirs_90 roan_90 swan_90 vir_90 ;
  do

  lab_let=$(echo -n $(printf "\\x$(echo $lab_num)"))

  echo "
  $lab_let
  $map_name
  ${map_name}.ps" ;

  echo $((lab_num++)) > /dev/null ;

  done
--snipped--

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")

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++))

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



Thanks,

Tyler



Since you're using bash, you may also find it convenient to put your hex digits into an array, which you can then subscript into with decimal numbers, to build the hex values needed to print other characters.

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)

tens=0
digits=0

while [ $tens -lt 3 ]
do
  while [ $digits -lt 16 ]
  do
    echo ${x[$tens]}${x[$digits]}
    ((digits++))
  done
  digits=0
  ((tens++))
done

The result is:

00
01
02
.
.
.
2D
2E
2F

Change the 'tens' and 'digits' as needed to get the right starting value.

Bob

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to