Archive for March, 2012

Bash working with the filename

March 23, 2012

In bash/sh scripting its very common that you are given a filename and have to perform various tasks on it. Following are some of the bash functions I created in order to perform them. Feel free to use them as you like.

Incase you dont know how bash functions work it is explained here. If you are only interested in how it is done just look at whats inside the function only. Thats where the magic happens.
#print the filename from the given path
get_file_name(){
 echo $(basename $1)
}

#print the filename without the extension
get_file_name_without_extension(){
 local filename=`get_file_name $1`
 echo ${filename%.*}
}

#print the filename extension
get_file_name_extension(){
 local filename=`get_file_name $1`
 echo ${filename##*.}
}

#print parent directory of a file
get_path_to_file(){
 echo $(dirname $1)
}

#print the absolute path of a file/directory given its relative path
get_absolute_path(){
 local relative_path=$1
 if [ -d "$relative_path" ]
 then
    local absolute_path=`cd $relative_path; pwd`
 else
 local file_name=`get_file_name $relative_path`
 local dir_name=`get_path_to_file $relative_path`
 local dir_name=`cd $dir_name; pwd`
 local absolute_path=`combine_path $dir_name $file_name`
 fi
 echo $absolute_path
}

#disguised to look like it just prints the number of files in a directory but infact it launches world war 3
count_files_in_dir(){
 echo `ls -1 $1 | wc -l`
}

Functions in bash/sh scripts

March 23, 2012

Its pretty simple to work with functions of commandline bash/sh scripts.

Define function

This is how you define a function
func_name(){     
  <bash command_1>     
  ....     
  <bash command_n>
}
eg:
say_hello(){  
  echo "Hello World"
}

Calling the function

In order to call the function the function needs to be defined as above before you actually call it. Then you just use the name of the function
eg:
say_hello(){
  echo "Hello World"
}

say_hello

Passing parameters to the function

Passing parameters to the function is pretty weird. Its the same as parameters being passed to a script. The function will have its own set of $* variable where the 1st parameter is $1, the next is $2 and so on.
eg:
say_hello(){
  echo "Hello $1 $2"
}

say_hello "Saminda" "Wijeratne"

Defining variables inside a function

Defining variables inside function is same as how you’d do usually in a script. Only difference is that if you want the variable to be local to the function you need to use the keyword “local” when defining it.
eg:
say_hello(){
  local first_name=$1
  last_name=$2           #becomes a global variable
  echo "Hello $first_name $middle_name $last_name"
}

middle_name="Saminda"               #global variable
say_hello "Kushan" "Wijeratne"
echo $firt_name
echo $last_name
Passing a string as a variable is easy. But passing an array to a function is pretty tricky. Here’s an example on how to do it.
eg:
say_hello(){
 eval "declare -A bio_data_array="${1#*=}
 #now bio_data_array is the array that was passed as a parameter
 echo "Hello ${bio_data_array['name']}"
}

declare -A bio_data
bio_data["name"]="Saminda"
bio_data["age"]=28
say_hello "$(declare -p bio_data)"

Returning values from the function

You’ll be surprised to know that bash functions never return anything. However you can capture what was printed by the function in the form of a string using the cool feature in bash where you can execute commands within “`” characters which the result is the output given by that command.
eg:
output_string=`say_hello $firt_name`
echo "This is what the say_hello function had to say: $output_string"
There are several other ways like using global variables or pass a variable name as a parameter to save the result of the function inside the function itself. But I prefer the above method since the function doesn’t have to worry about whether or not its a function or not.

Mounting a file as a file system in Linux

March 21, 2012

This is called mounting a loopback device.

3 steps.
  1. Creating the file that can be mounted as a file system
  2. Assigning the file as a block device
  3. Mounting the block device to a path
Creating the file that can be mounted as a file system

Use the dd command to create an empty file of the size which you want your file system.
  $ dd if=/dev/zero of=my_fs bs=1024 count=30720
of – parameter specifies the name of the file
bs – size of the block (represents x no of KB. You can specify x no of MB using xM: i.e. “bs=1M”)
count – how many blocks to be present (thus the size of the file system = bs*count)
if – from which device the file content initially should be filled with.
But still the file that gets created is not a proper filesystem yet. you need to format it using some file system type. In order to do that first lets assign the file to a loop back device (the next step)
Assigning the file as a block device

In linux a file system has to be a block device in order for it to be managed as a file system. In this particular case we use a set of defined loop back devices for our purpose. eg: /dev/loop0 up to /dev/loop8
in order to check if a particular loop back device is already being used use the command,
  $ losetup /dev/loop0
If its already being used you will see it mentioned in the output of the above command. Once you find a free loop back device to attach/assign the file to it
  $ losetup /dev/loop0 my_fs
This command needs to be issued as a sudoer user.
Now lets format the loopback device we just attached. 
$ mkfs -t ext3 -m 1 -v /dev/loop0
Effectively we are creating a filesystem inside a file using the above command. I’m formatting it as ext3 file system. You may have to use sudo again.
Mounting the block device to a path

The hard work is done. Now to just mount.
  $ mount -t ext3 /dev/loop0 /mnt/fs_mount_location/
You may ommit the “-t ext3” parameter in this case since usually linux file systems are automatically detected at mount. You may have to sudo the above command. 
Thats it actually. Once you are done you should clear up after you ofcourse. to do that just 2 commands
  $ umount /mnt/fs_mount_location/
to unmount. you may need sudo again
  $ losetup -d /dev/loop0
to detach the file from the loop back device. Need sudo again.
Steps are curtasy of http://www.walkernews.net/2007/07/01/create-linux-loopback-file-system-on-disk-file/

3 Lessons I never seem to learn

March 13, 2012
  1. Never trust women
  2. Never leave your work until the last minute
  3. Try to learn the 3 lessons I never seem to learn

Listening to something you already knew

March 4, 2012

I always knew that knowing something and understanding something is totally different. But I never thought that knowing something and listening to what I know can feel totally different.

 

Today I read a blog post of a friend of mine… Or perhaps more elaborately speaking who used to be a friend of mine… to be more exact a person who is at the other end of the path filled with alot of broken glass. I knew who she was, the kind of person who she really is before she changed (or was it I who changed?). I don’t know at which point I knew those stuff.. or how I knew for that matter. Perhaps because I’ve being in the same shoes… same fears, same doubts… but somehow that did not really connect us. I guess it made us feel that we are related. Like fruit from the same tree (but in different branches).

 

Ever being in front of a crowd which exceeded the hall? over 300 pairs of eyes following your every movement. When you take the Mic the slightest scratch on your head.. the smallest stutter… gets noticed like its the main highlight of the evening… At that precise moment they’ve no idea how hard it is to look at the audience without having your head feeling dizzy… stop your hands shaking of feeling cold… trying to utter words out of a mouth that all of a sudden has gone dry…. What in the world am I doing here… 

 

It was about a public speech she made last year. The very first one to a variety of people of such numbers. I was sooooo proud of her on what she is about to do… Felt like “my little girl all grown up”. Felt like all the work over the years led to this… To this one single moment of my achievement through her. My achievement in being part of making her who she is… I got the rare feeling of a feeling of being swelled with pride. It felt like a dream she had said. Back then we were still friends… there were some large cracks in the glass, but still friends…

Reading the post was one of the happiest and disappointing post I’ve ever read. I don’t know why but feels that both are the same.  She had done her speech splendidly… Happy because it showed how she came out of her shell to see the world. To know that she can achieve anything and everything is possible if she wants to. I could almost imagine how she was smiling when she typed those words. There’s a weird thing about achievements. It comes with a cost. “Overconfidence” one might say… “misguided” one might judge… “lost” one would proclaim… but I’d say disappointment and leave it at that (going further will not help anyone).  bcos less than a week later she shattered our friendship of had remained in to dust.

 

Friendships don’t last forever… Something which I already knew… but turns out it doesn’t matter that you did…