Tag Archives: Coding

Emulating a Developer: Part 3 – Linux coding (“bashing away”)

Being the ongoing adventures in application development … by someone who really doesn’t develop code.

<Continues from Part 2>

Thus far I had ‘designed’ my simple program. Well, in my head at least. Then built a working PC (vbscript) version by my tried-and-true learn then code method. But it was slow and had annoying security warning popups. 

So I decided to start again and use the same ‘design’ but write it as a BASH shell script that runs directly on the Linux Server. This took a lot less time to do, but I still had to learn – actually re-learn – how BASH does things like input strings, break them up into individual values, add things up, loop around, create random values, merge strings etc. 

But it wasn’t that hard at all. A few hours later and here it is:

# creates a suitable rar command to break up large file(s) into 7, 14 or 21 smaller rar files
# pass in file sizes in MB and it will do the rest.
# example: crtRar 350 200 400
# results in:  sum is: 950 MB     using 14 parts…  rar a -v67m -m0 -R ~/temp/dio.rar
# just copy and paste and add your (3) file names at end of command

let mySum=0

# pass in 1 to n values. being file sizes in MB

for i in $*
let mySum=$mySum+$i
echo "sum is: "$mySum" MB"

# Generate simple 3 char random file name for .rar file
rarName=`head -c 500 /dev/urandom | tr -dc a-z | head -c 3; echo`

# Simple "if" rules, such if mySum (file size totals) le 700 (MB) then 7 parts, etc
if [ $mySum -le 700 ]; then
    let rarSize=$mySum/7 ; echo "using 7 parts…"
elif [ $mySum -le 1400 ]; then
    let rarSize=$mySum/14 ; echo "using 14 parts…"
    let rarSize=$mySum/21 ; echo "using 21 parts…"
# finally build and show rar command
rarCmd="rar a -v"$rarSize"m -m0 -R ~/temp/"$rarName".rar "
echo $rarCmd


Even I can see it could do things smarter. But it’s Version 1, it works and so I’m happy. Plus I am still not a developer…

PS: Things I want to add in Version 2:

  • Pass the file or folder names per se and have the program use them to work out the rar part size
  • Then add the provided file/folder name to the (end of the) rar command

Emulating a Developer: Part 2 – PC coding (“what, no clipboard?!”)

Being the ongoing adventures in application development … by someone who really doesn’t develop code.

<The first part is, ironically, located in Part 1>

So my first cut, simple, crtRar utility has to do this:

  1. Call the program, passing the 1 or more file sizes in MB (eg “crtRar 600 100”)
  2. Break up the input ‘string’ (600 100) into individual values, i.e two in this case
  3. Add them up to give total filesize
  4. If this total filesize was 700 or less, I’ll break up into 7 equal parts. If between 700 and 1400 then 14 parts. If greater than 1400 then 21 parts
  5. Calculate the rar file part size as per 4. above
  6. Create a random, 2 letter file name
  7. Build the rar command using the output of 5 and 6 etc
  8. Put it up on the screen, ready to be copied and pasted…and used.

I decided to develop it on my Windows PC and opted for the ‘in built’ language called vbscript.  I don’t really know this language, but what better way to learn than dive in and code. Plus I could store the finished .vbs file on my Dropbox and have it automatically available on the other PCs.

So I literally created a new Test.vbs file in Notepad++ and thought, right:  Step 1 is “pass in the 1 or more file sizes…”  I wonder how vbscript does that?  Looked it up on Google.  Worked out how to write out results to the screen (so I could test things on the fly).

I got Step 1 coded, so then it was Step 2.  How do I break up that string into individual values.  Hello again friend Google.

Continue reading

Emulating a Developer: Part 1 – Learn as you type

Being the ongoing adventures in application development … by someone who really doesn’t develop code.

I never actually learnt how to write code. I can’t recall ever even vaguely doing it for a living. But ever since I can remember, I’ve dabbled and whipped up utility applications for my own use.

I was amazed to discover, years after leaving IBM, that one of my little utilities was still being used internally by some people.  Anyway, I had cause to whip one up just today. And the more things change, the more they stay the same.

I still learn as I type. That is, I don’t sit down and learn the language I’m writing in, then work out the design, then think it through some more, then actually start coding.

Nope. I get the Big Picture in my head, then dive in.  I’m picturing, in my head, a flow chart. Don’t write things down!   Then off we go entering the code for “crtRar

What crtRar had to do – the basics

My crtRar (“Create RAR”) program actually serves a valuable purpose.  I have some big files up there on a server and I want to get them down to the PC. It’s an FTP server; actually SFTP.  Say, the two files are:

  BigBuckBunny.avi  600 MB

  Test22.mpg 100 MB

The FTP server will only allow each download ‘stream’ to be about 200 kBps. So I can download the 2 files at the same time, but each one will only be at 200 kBps. That 200 kBps is about 1/7 of my total ADSL bandwidth. So it’s not efficient at whatever 2/7 as a percent is .

Both the PC and the FTP server (running Linux) support the RAR compression program. It’s a bit like zip or winzip, with one difference. It can easily break up 1 or more files into multiple files.  Can you see what’s coming next?

Take the two files (total of 700 MB) and create 7 different RAR files. Make them all the same size (700/7 = 100 MB) and then download all 7 at once, that is 7 FTP streams.  Will download much quicker than just the 2 original files. (Actually there’s also no point in compressing the data as both are already compressed, so we can save time by telling RAR not to pack the data.).  On the server this is a sample of the command:

  rar a -v100m -m0 -R ~/temp/zr.rar BigBuckBunny.avi  Test22.mpg

Won’t go into details, but it creates the multiple (7) x 100 MB rar files  zr.part01.rar, zr.part02.rar etc  (I chose the name “zr” at random, means nothing!  The command does a bit more, like catering for whole directories etc)

Continue reading