Re: bash scripting question



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



Relevant Pages

  • Re: PATH variable
    ... Floyd L. Davidson wrote: ... >>which should result in echo $PATH ... configuration files before bash invokes /etc/profile ... > come to a single line in a single file that in fact does modify ...
    (comp.os.linux.misc)
  • Re: Bash script - telnet
    ... > I need a telnet script in bash. ... (echo user your_username ... Notice that "echo" preceeds the commands to the remote server. ...
    (comp.unix.questions)
  • Re: Probably very stupid script/bash question
    ... both solutions work perfectly under bash. ... echo "Data received = $E Bytes" ... If the first line has white space, then as many variables as there are "fields" in the line will contain values. ... you must have one or more white spaces between the two 'less than' symbols and NO space between the 'less than' and the 'left parenthesis'. ...
    (Debian-User)
  • Newlines dropped? Despite double quotes?
    ... Why does this put the digits all on one line, ... Shouldn't they be separated by a newline (with an initial ... I tried it with $(echo) too - same result. ... $ bash --version ...
    (comp.unix.shell)
  • Re: Error returns not handled correctly by sysfs.c:subsys_attr_store()
    ... Andrew Patterson wrote: ... Used /bin/echo instead of the built-in echo command. ... Zsh and Bash both fail. ... AMD running Debian lenny with i686 kernel -- fails. ...
    (Linux-Kernel)