looping in hexadecimal with bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looping in hexadecimal with bash
# 1  
Old 07-01-2008
looping in hexadecimal with bash

i want to write a script that creates a directory tree named in hexadecimal but i'm stuck at the hexadecimal part ... here is my code (incase i was dealing with intergers ...using bash)
Code:
max=39
for((i=1;i<=max;i++))
  do 
    mkdir $i
    cd $i
done

i have tried using "typeset i16 i" but this gives me an error in bash but it works with ksh yet it names my directories with the "16#" part in ksh ...
any suggestions on how i can improve my code ... and can i make it work in bash and ksh esp bash ... i've seen posts like mine on the topic of hexadecimals but, ksh was the remedy.

Last edited by radoulov; 07-01-2008 at 12:35 PM.. Reason: added code tags
# 2  
Old 07-01-2008
Use CODE tags around source listings.

Code:
max=39
for((i=1;i<=max;i++))
do
  i=$(printf "%#x\n" $i)
  mkdir $i
  cd $i
done

# 3  
Old 07-01-2008
thanks

thank you shamrock i was just about to ask you what
Code:
i=$(printf "%#x\n" $i)

does but i think i have figured it out .
# 4  
Old 07-05-2008
how do i improve to two digit

Quote:
Originally Posted by shamrock
Use CODE tags around source listings.

Code:
max=39
for((i=1;i<=max;i++))
do
  i=$(printf "%#x\n" $i)
  mkdir $i
  cd $i
done

how can i improve the code so that the hexadecimal names are two digit thus the directories are
Code:
00 01 02 ..FF

instead of
Code:
0 0x1 0x2 ..

# 5  
Old 07-05-2008
The printf statement should looks like:

Code:
printf "%X\n" $i

Regards
# 6  
Old 07-07-2008
Replace the "#x" with ".2X"
Removing "#" gets rid of the "0x" prefix and ".2" forces all to 2 digits even the ones that are singular.
Uppercase x gives uppercase hex digits.
Code:
i=$(printf "%.2X\n" $i)

See the printf manpage.
# 7  
Old 07-08-2008
error encountered

Quote:
Originally Posted by shamrock
Replace the "#x" with ".2X"
Removing "#" gets rid of the "0x" prefix and ".2" forces all to 2 digits even the ones that are singular.
Uppercase x gives uppercase hex digits.
Code:
i=$(printf "%.2X\n" $i)

See the printf manpage.

thanks but if i try that, the scrpt cannot create beyond 08 the shell gives the message
Code:
((: 08: value too great for base (error token is "08")

..try running the loop to create say 18 directories and see if it will run, i've gone through printf man but i'm yet to solve this other new problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate hexadecimal

Hello I'm working on a script to list all ipv6 from given address so I've run this script which create hex part of ipv6 STR2=159 END2=200 SUM2=`expr $END2 - $STR2` for ((i=STR2;i<=END2;++i)); do x=$( printf "%x" $i ) ; echo $x echo -e "::"$x >> netpart.txt done output is : ::9f... (2 Replies)
Discussion started by: nimafire
2 Replies

2. Shell Programming and Scripting

Bash Script Looping all the time

Hello, I have a database file, named data.txt, and a shell script (convert.sh) to convert data.txt from columns to row. Output file name will be column_to_row.txt In this example data.txt has only four rows. Format of data.txt is: info name surname telefon_nr Data.txt info boris... (1 Reply)
Discussion started by: baris35
1 Replies

3. 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

4. Shell Programming and Scripting

Looping structure to make up for lack of bash GOTO

Hello, I am re-processing some files when a specific condition is met. The condition is read from the filename. Since files may need to be re-processed a number of times before they no longer meet the condition, I need to know when to stop re-processing. I am having trouble visualizing the... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

5. Shell Programming and Scripting

Looping Bash Script

Does anyone have a same of a bash script that cd to a directory and execute a cgi script then moves onto the next directory then executes another cgi ? (3 Replies)
Discussion started by: Virusbot
3 Replies

6. Shell Programming and Scripting

hexadecimal replacing with awk ?

Hi there ! I have text files with some nonsense characters in it, so different text editors put different nonsense symbols, and, worse, the application that should be able to read these files doesn't. With xxd, the nonsense characters show as "efbfbd", while they should be "c2a7" (the... (2 Replies)
Discussion started by: jossojjos
2 Replies

7. Programming

Hexadecimal to ascii

Let's suppose i have a hexadecimal array with 16 cells.for example b3e2d5f636111780 i want to convert it to an array of ascii characters(in C) so that i can reduce total size of the file i want to put it in. But i am afraid i have not fully understand the difference between ascii and hex(i... (3 Replies)
Discussion started by: bashuser2
3 Replies

8. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

9. UNIX for Dummies Questions & Answers

looping in hexadecimal with bash

hello every one this is my first post ... well i'm new to linux .... i've been enjoying shell scripting tutorials and i'm new to writting scripts i want to write a script that creates a directory tree named in hexadecimal but i'm stuck at the hexadecimal part ... here is my code (incase i... (2 Replies)
Discussion started by: soba
2 Replies

10. Shell Programming and Scripting

Get Hexadecimal Value

I have a record in a file that ends with the new line character "\n". How dio I determine the hexadecimal value for that? (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question