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?
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?
to start with:
echo 'obase=ibase=16;1A+1' |bc
Hi sai_1712,
One way using perl:
$ perl -e '$var = 0x01A; for ( 1 .. 5 ) { printf qq[%03X\n], $var++ }'
01A
01B
01C
01D
01E
Regards,
Birei
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
#!/bin/ksh
incr=3
while read val
do
echo "obase=ibase=16;${val}+${incr}" |bc
done < lun.txt
Using bash:
#!/bin/bash
incr=4
while read val
do
printf "%X\n" $((0x${val}+incr))
done < lun.txt
or perhaps your after:
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))
Hello vgersh99,
When I use the below code I am getting the below output.
#!/bin/bash
incr=3
while read val
do
echo "obase=ibase=16;${val}+${incr}" |bc
done < lun.txt
Output:
1D
***********************
lun.txt file contains
01A
***********************
I am looking for the below output. The increment is variable and to be defined by the user every time depend on requirement
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
#!/bin/bash
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))
#!/bin/bash
incr=4
while read val
do
printf "%X\n" $((0x${val}+incr))
done < lun.txt
Output:
syntax error at line 5: `(' unexpected
#!/bin/bash
read -p "Enter increment: " inc
read -p "Enter HEX value: " hex
printf "HEX + increment is: %X\n" $((0x$hex+inc))
Output:
-p: is not an identifier
Building on the above code it became necessary to test the script for a larger number of numbers because the arithmetic in the "bc" is all in hexadecimal. The "bc" command expects upper-case hexadecimal. Some adjustment to output to put the leading zero on your hex output (which is unconventional).
max=20 # Different numbers to generate
while read val
do
incr=0
while [ ${incr} -le ${max} ]
do
# Convert $incr to upper case hexadecimal for "bc"
incrx=$(printf "%#x\n" ${incr}|sed -e "s/x//g"|tr '[a-z]' '[A-Z]')
hex=$(echo "obase=ibase=16;${val}+${incrx}" |bc)
printf "0${hex}\n"
incr=$((${incr} + 1))
done
done < lun.txt
01A
01B
01C
01D
01E
01F
020
021
022
023
024
025
026
027
028
029
02A
02B
02C
02D
02E
This code didn't execute. Got the below error..
#! /bin/bash
max=20 # Different numbers to generate
while read val
do
incr=0
while [ ${incr} -le ${max} ]
do
# Convert $incr to upper case hexadecimal for "bc"
incrx=$(printf "%#x\n" ${incr}|sed -e "s/x//g"|tr '[a-z]' '[A-Z]')
hex=$(echo "obase=ibase=16;${val}+${incrx}" |bc)
printf "0${hex}\n"
incr=$((${incr} + 1))
done
done < lun.txt
syntax error at line 9: `incrx=$' unexpected
Since read -p "prompt" var and $() dont work with your version of bash it must be quite old.
What OS are you on? Can you show us the output of:
bash -c 'echo $BASH_VERSION'
on your system?
2.03.0(1)-release
Unmodernised version for Bourne Shell of the day:
max=20 # Different numbers to generate
cat lun.txt | while read val
do
incr=0
while [ ${incr} -le ${max} ]
do
# Convert $incr to upper case hexadecimal for "bc"
incrx=`printf "%#x\n" ${incr}|sed -e "s/x//g"|tr '[a-z]' '[A-Z]'`
hex=`echo "obase=ibase=16;${val}+${incrx}" |bc`
printf "0${hex}\n"
incr=`expr ${incr} + 1`
done
done
Yes I have noticed the "cat".
The above code is working... Thank you all for your help!!!!!!!
Wow! 2.03 is over 12 years out of date, it might even have Y2K issues ![]()
At last I cannot be criticised for posting perfectly normal Shell script, complete with backticks, "expr" and "cat". This used to be quite normal in system-supplied startup scripts.