Thursday, August 29, 2013

Cutting a string from shell script

Today i am showing a way to cut specific word from a string in linux shell script,

lets u have  a file such as

Alex Farguson manchester united coach 1987-2013

now we want to get only the year when he started his job, that is 1987, say the file name is coach.txt

we can now simply use,
cat coach.txt | awk '{ print $6 }' | cut -f1 -d"-"
  • cat coach.txt will print the contents of the file to stdout.  
  • awk '{ print $6 }' will print the sixth column, i.e. the one containing 1987-2013
  • cut -f2 -d"-" will cut the output of previous step using - as the delimiter and get the first column (-f1)
-->

Floating point calculation in linux shell script

BC is a useful utility/language to do floating point calculation in linux shell script.

it has so many formats, here are two examples,
bc <<< 20+5/2
bc <<< 'scale=4;20+5/2'
 
here scale=4 means we will consider decimal up to four digits.

another example may be,
 
echo "scale=4;$a/$b" | bc
 
details here.
 
  
-->

Tuesday, August 27, 2013

How to upload files or folders to dropbox from the command line in any linux machine (An engineer's thought, :P)

I was in a complex situation. I needed to transfer a folder of size 5 GB from server A to server C. The problem was that i can only access server A from server B, and server B from server C.

I could transfer the folder from A to B by scp, and do the same thing again for server B and C. But the problem was that I was limited in disk space in server B. So i tried several methods such as pipes, tunneling, port forwarding. But I failed and i did not have enough time to debug. Then suddenly an idea came to my mind, afterall I am an engineer, i should have found out simple work arounds, as Bill gates said, I prefer lazy persons as they will find an way to do things easily, lol.

Here is what I did, I used a bash script that allowed me to upload files from A to drop box directly over command line. Then i just downloaded that folder to my local server C. Here is the link of the dropbox uploader script, https://github.com/andreafabrizi/Dropbox-Uploader. The only limitation is individual files should be under 300 MB. The process is simple and described in the above page.

I thought I should share this with you guys. :)

-->