Hexadecimal increment using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hexadecimal increment using shell script
# 1  
Old 01-10-2012
Hexadecimal increment using shell script

I am looking for hexadecimal increment by reading user input using shell script. For example the user input is 01A. I want to increase the hex count by 4 times

Looking for output:

01A
O1B
01C
01D
01E

Can someone suggest?
# 2  
Old 01-10-2012
to start with:
Code:
echo 'obase=ibase=16;1A+1' |bc

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-10-2012
Hi sai_1712,

One way using perl:
Code:
$ perl -e '$var = 0x01A; for ( 1 .. 5 ) { printf qq[%03X\n], $var++ }'
01A
01B
01C
01D
01E

Regards,
Birei
# 4  
Old 01-11-2012
Thank you vgersh99 & birei for suggestion...

I am new to shell scripting. This hexadecimal increment count is part of my customized script. I get the output like below when i run my script

Cat lun.txt
01A
01A

The line1 & line2 values in lun.txt are same.The lun.txt output varies when ever I run my script and the increment count also varies it may be 2 or 3 or 4 depend upon requirement

I want to call the line1 variable(hexadecimal) from lun.txt file and increment by 2 or 3 or 4......... times which will define by user.

Your suggestions will be more helpful to complete my customized script.

Please note that I've written my script in shell
# 5  
Old 01-11-2012
Code:
#!/bin/ksh

incr=3
while read val
do
   echo "obase=ibase=16;${val}+${incr}" |bc
done < lun.txt

# 6  
Old 01-11-2012
Using bash:

Code:
#!/bin/bash
incr=4
while read val
do
  printf "%X\n" $((0x${val}+incr))
done < lun.txt

or perhaps your after:
Code:
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))


Last edited by Chubler_XL; 01-11-2012 at 08:43 PM..
# 7  
Old 01-17-2012
Hello vgersh99,

When I use the below code I am getting the below output.
Code:
#!/bin/bash
incr=3
while read val
do
   echo "obase=ibase=16;${val}+${incr}" |bc
done < lun.txt

Output:
Code:
1D

***********************
lun.txt file contains
Code:
01A

***********************


I am looking for the below output. The increment is variable and to be defined by the user every time depend on requirement
Code:
01A
01B
01C
01D

---------- Post updated at 04:06 PM ---------- Previous update was at 04:00 PM ----------

Hello Chubler,

The below code is not working. Please let me know where I've to modify.The increment is a variable and to be defined by the user every time depend on requirement
Code:
#!/bin/bash
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))

Code:
#!/bin/bash
incr=4
while read val
do
  printf "%X\n" $((0x${val}+incr))
done < lun.txt

Output:
Code:
syntax error at line 5: `(' unexpected


Code:
#!/bin/bash
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))

Output:
Code:
-p: is not an identifier


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-17-2012 at 06:54 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

6-digit hexadecimal ID in shell

Hi, i tried to do this script: Generate a "unique" 6-digit hexadecimal identifier for your computer. Do not use the flawed hostid command. Hint: md5sum /etc/passwd, then select the first 6 digits of output. Fom 0 to 9 and from a to f #!/bin/bash clear echo "" echo "--------------------"... (4 Replies)
Discussion started by: jose2802
4 Replies

2. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

3. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

4. Shell Programming and Scripting

help for a script to increment time in hh:mm:ss format

hi all, I need help for incrementing time in hh:mm:ss format like for example : when i have a values like 02:07:16 I want to increment this value with 5 secs till it reaches a condition. Can someone help me with this. Thanks in advance. (2 Replies)
Discussion started by: hitha87
2 Replies

5. Shell Programming and Scripting

Increment date in script

Hi, Iam new to scripting language.:o can someone help me out solving this thread?hopingly ya....:) I want to write a script which connects to db and searches the count in a table which has date column and id column. If the count is not equal to 0 then it should increment the date with the one... (4 Replies)
Discussion started by: jyothi_wipro
4 Replies

6. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

7. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

8. Shell Programming and Scripting

Shell Script To increment Date*HElp***

Hi, I want to increment date, using Shell Script, a loop to add one day after 24hrs. :confused: Cheers Kunal (1 Reply)
Discussion started by: niceboykunal123
1 Replies

9. UNIX for Dummies Questions & Answers

script for nums in Hexadecimal base

Hi: I´ve developed a script that count in decimal base. this is a example: ini=0 end=0 ini=$1 end=`expr $2+1` i=$ini while do .. .. i=`expr $i+1` done Could you say me how dou you count in hexadecimal base? Thanks (1 Reply)
Discussion started by: AlvaroD
1 Replies
Login or Register to Ask a Question