Hi to all in forum,
I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:
LetterNumber=10
printf "\x$(printf %x $((${LetterNumber}+64)))"
$ J
#The problem, how to assign printf output (J in this case) to a variable?
LetterNumber=10
set Letter=printf "\x$(printf %x $((${LetterNumber}+64)))"
echo $Letter
$
I've tried with part in blue, but I get a blank output.
Any help would be very appreciated
PS: Any optimatization of the code I have will be welcome, because It seems a little slow command I have.
Grettings
On Linux use the -v option to assign printf output to a variable.
LetterNumber=10
printf -v variable "\x$(printf %x $((${LetterNumber}+64)))"
echo ${variable}
Regards,
SRG
Or as perl one liner:
perl -e '$number=3; @hash{1 .. 26} = ('A' .. 'Z');print "letter= $hash{${number}}\n";'
As a perl script:
#!/usr/bin/perl -w
use strict;
my $number="26";
my %hash;
@hash{1 .. 26} = ('A' .. 'Z');
print "letter= $hash{${number}}\n";
Regards,
SRG
Hi SRG,
Correct!!, it works. Many thanks.
Only to use the variable content I need to use it in a for loop,
but is not working in this way.
LetterNumber=10
printf -v letter "\x$(printf %x $((${LetterNumber}+64)))"
for i in {A..${letter}}; do
echo $i
done
the for loop should be from A to J, but instead of get the A,B,C,D,E,F,G,H,I,J, I get {A..J}.
How would be the correct syntax to use variable letter within for loop?
Thanks for help so far.
Regards
You could try it this way....
LetterNumber=10
for i in $(seq 1 ${LetterNumber})
do
printf -v letter "\x$(printf %x $((${LetterNumber}+64)))"
echo ${letter}
done
Personally, I would more than likely use perl as it is more cross platform friendly. For example the -v switch does not appear to be available in Solaris10 bash printf command, which in itself is a little weird as printf is internal to bash, so I would have thought it should be pretty much the same across bash instances on various OS but hey ho....
Regards,
SRG
Thanks SRG,
I'm trying as below but I receive syntactix error, I'm not sure why:
LetterNumber=10
for i in $seq(1 ${LetterNumber})
do
printf -v letter "\x$(printf %x $((${i}+64)))"
echo ${letter}
done
Ophiuchus,
I had a typo $seq( should be $(seq
I have updated my original post with the change.
It works in this way:
LetterNumber=10
for (( i=1;i<=${LetterNumber};i++ ))
do
printf -v letter "\x$(printf %x $((${i}+64)))"
echo ${letter}
done
Many thanks for your help SRG.
Grettings