starkk92
starkk92

Reputation: 5924

How to store hex value in shellscript

I am new to shellscript

I have this program

for(( s=0x001; s <=0x00f; s++))
do
echo $s
done

I want to print s values as 1,2,3,4.....a,b,c,d,e,f

But when I run above program,I have seen the output as 1,2,3,4,5,6......13,14,15.

I want to print the hex values only.

How to pass hex values in pipe.Lets say I have to pass this hex in pipe along with some other arguments.How to do that?

Lets says I have to pass some arguments to access device driver in pipe.

echo "8 $s q" | /usr/sbin/tejas/test /dev/slot$1/tupp_fpga$devNo | grep "at offset"

Here s should contain hex values.How to do it.

Upvotes: 0

Views: 2445

Answers (3)

Sylvain Leroux
Sylvain Leroux

Reputation: 52030

I'm not quite sure to understand the updated question but...

... if you require the given format, something like that will do the job:

print "8 %x q" $s | /usr/sbin/tejas/test /dev/slot$1/tupp_fpga$devNo | grep "at offset"

If your not familiar with the printf-style functions please refer to one of the many web pages describing the various format available.

On this particular case, the %x in the format string will be replaced by the hexadecimal representation of the next argument. Given that the s variable hold the value 1010 (or 0xA16 or 0138 -- which are indeed the same), the printf internal command will write the string 8 a s to the standard input of your /usr/sbin/tejas/test tool.

Upvotes: 0

zoul
zoul

Reputation: 104065

I think printf "%x" $s should do. It is part of the POSIX standard, and implemented as a built-in command in bash:

$ type printf
printf is a shell builtin

…so the documentation can be found in man bash, at least for the Bash shell.

Upvotes: 3

pdw
pdw

Reputation: 8851

Use the printf command. It's standard and works more or less like the C function.

printf "%x\n" $s

Upvotes: 3

Related Questions