[uae] Re: Very ugly hdfcreator in bash

  • From: elmar@xxxxxxxxxxxxxxxxxx
  • To: uae@xxxxxxxxxxxxx
  • Date: Thu, 30 Jun 2005 11:28:33 +0200

Hi Gingat! 
> Gingat Florent wrote:
[snip]
>  Elmar can you explain me a little this =
> 
> count=*`**echo* $2 "2048  * p q" *|* *dc* -*`
> 
>  I mean i've understand the echo $2 (2 come from the second input) but "2048 
> * p q" 
> 
>  I d'ont understand it's a multiplication but what * what :p ? 

Sorry, maybe "quoted-printable" was the wrong choice for the attachment. 
I'll paste the script at the bottom of this mail.

Calculations in the shell are a bit tricky. The least common denominator seems 
to be the 'dc' command (desktop calculator?) which features some reverse polish
notations (like in forth or postscript ...). 

Unfortunately dc expects a file which contains the appropriate commands. Now, 
the special file name '-' means standard input. So the following commands get 
piped into dc via the echo command
(in human readable form)

Push $2 (the second argument of the script) onto the stack
Push 2048 onto the stack
Multiply (*) the two top-most values on the stack (leaving its result on the 
stack)
Print the top-most value on the stack (p)
Quit (q)

The reverse ticks `...` allow us to save the output of this command 
(the number printed) in $count. 
 
Browsing through the bash manual I just found the chapter "Arithmetic 
expressions".
The command

let count=2048*$2

also seems to do the job. But then the script wouldn't be ugly any more :)
(and it would only work with a bash).

Regards,
        Elmar.
---------------------------------------
#!/bin/bash
echo; echo "HDFCreator v0.01 fgingat__at_____gmail__dot__com, v0.1 e. plischke"

if [ $# -ne 2 ]; then
 echo $0 " needs arguments name and size (in MB)"
 exit 1
fi

if [ -e $1 ]; then
 echo File $1 exists.
 exit 1
fi


count=`echo $2 "2048  * p q" | dc -`
dd if=/dev/zero of=$1 bs=512 count=$count

exit 0


Other related posts: