Sunday 20 March 2016

Double digit Arguments Passed to Shell script - Tricky Scenario

Guys, you know that $# is a special variable that gives the arguments passed to the shell script.

$1 will give the 1st argument passed, $4 will give the 4th argument passed to the script.

Let's consider a tricky scenario. 
Note : This is a tricky interview question.

Let's say you want to pass 12 arguments to the script.

Did you notice that after 9th argument, all arguments passed would be of 2 digits... I mean the 10th, 11th and 12th argument.

So let's say to get the 12th argument, you may tend to use $12. But hey look again! Thats not right! The script doesn't understand that. Check out a practical example to know what I mean.

Let's take a simple bash script and compare the output of Wrong and Correct versions.

[WRONG VERSION]

#!/bin/bash

# Call this script with 12 parameters

echo "1st parameter is $1";
echo "2nd parameter is $2";
echo "3rd paramter is $3";
echo "4th parameter is $4";
echo "5th paramter is $5";
echo "6th parameter is $6";
echo "7th parameter is $7";
echo "8th paramter is $8";
echo "9th parameter is $9";
echo "10th parameter is $10";
echo "11th parameter is $11";

echo "12th paramter is $12";

Now lets say you execute the script with 12 arguments. Watch the output :

./Parameters_Pass.sh 5 10 15 20 25 30 32 35 42 45 50 65

1st parameter is 5
2nd parameter is 10
3rd paramter is 15
4th parameter is 20
5th paramter is 25
6th parameter is 30
7th parameter is 32
8th paramter is 35
9th parameter is 42
10th parameter is 50
11th parameter is 51
12th paramter is 52

See how the 10th 11th and 12t argument that we passed got screwed up. Instead of 45, 50, 65 what we see is 50, 51 and 52. Thats's so wrong, right?

Now here's the correct version of the script.

[CORRECT VERSION]

#!/bin/bash

# Call this script with 12 parameters

echo "1st parameter is $1";
echo "2nd parameter is $2";
echo "3rd paramter is $3";
echo "4th parameter is $4";
echo "5th paramter is $5";
echo "6th parameter is $6";
echo "7th parameter is $7";
echo "8th paramter is $8";
echo "9th parameter is $9";
echo "10th parameter is ${10}";
echo "11th parameter is ${11}";

echo "12th paramter is ${12}";

Observe the brace brackets used for 10th, 11th and 12th argument. Now if you execute the script with 12 arguments again, we'll get the correct output as expected.

./Parameters_Pass.sh 5 10 15 20 25 30 32 35 42 45 50 65

1st parameter is 5
2nd parameter is 10
3rd paramter is 15
4th parameter is 20
5th paramter is 25
6th parameter is 30
7th parameter is 32
8th paramter is 35
9th parameter is 42
10th parameter is 45
11th parameter is 50
12th paramter is 65

Saturday 19 March 2016

List only hidden files in Unix / Mac OS X

Folks you may be aware that ls -a will list all hidden files and visible files as well.

But to list only the hidden files, type the following in Terminal :

ls -ld .?*

This is one of the tricky interview questions, so make a note of this.

Friday 18 March 2016

Mac OS X - Hidden Gems : Part 2

Oh boy! yet another Mac OS X hidden gem that you probably weren't aware of.

Type this is Terminal to read Mrs Fields secret Cookies recipe.

open /usr/share/emacs/22.1/etc/COOKIES

Mac OS X - Hidden Gems : Part 1

Guys, presenting a mind blowing Mac OS X hidden gem that I just stumbled onto.

Type the following in Terminal to read some Apple supplied Jokes :)

open /usr/share/emacs/22.1/etc/JOKES

Thursday 17 March 2016

How to change Bash History Size on Mac OS X

Guys, I hope you must be aware, that the commands that you type in terminal get saved in the history. On Mac OS X, the default size of commands history is 500 i.e it saves the most recent 500 commands that you typed in terminal in a special file named "bash_history".

Open Terminal and check for yourself.

vi ~/.bash_history

Hit escape and type this to see the history limit
:set number


Now to change this default limit of 500 to something else, lets do the following in Terminal.

sudo vi ~/.bash_profile

Enter your password.

Add this line to the file :

#history
HISTFILESIZE=1000

That's it. It means from now on, it will save upto 1000 latest commands that you typed in history. Ciao!

Wednesday 16 March 2016

GRE GMAT CAT Wordlist - TurboPack 2

Guys, in our previous TurboPack, we saw the first set of wordlist for your reference. From this post onwards, lets rename the "Set" of words to something more appealing - "TurboPack". The random non-alphabetical sequence of words in the pack is intended for stronger and better memory retention.

TurboPack 2

uncanny : strange, mysterious
recluse : loner, someone living a solitary life
penchant : strong inclination or liking to do something
palate : sense of taste
visage : face, facial appearance
occult : mysterious, supernatural, mystical
medley : mixture, motley
jaunt : short trip, short excursion
indolent : lazy
torque : force producing rotation

Courtesy : Barron's and Google

Cheers!

Monday 14 March 2016

How to find size of Hashes in Perl

Hashes as you know are basically key-value pairs. So in Perl, if you want to find the size of hashes, check this out.

Consider a simple practical real-time Perl script for storing stock quotes of various companies.

#!/usr/bin/perl

use strict;
use warnings;

my %stock_quotes;
my @keys;
my $size_of_hash;

%stock_quotes=(
Apple => 102,
Google => 720,
Microsoft => 53,
Yahoo => 34,
Walmart => 67
        );

@keys = keys %stock_quotes;

$size_of_hash = @keys;


print "Stock hash size : $size_of_hash \n";

You see the %stock_quotes is the hash which has key value pairs i.e stock quote values in this case. Now, we store all the keys of the pairs in an array @keys. Note am not storing the values here, just the keys and we then find the size of this array @keys holding all the keys. Thus basically showing the size of the hash with $size_of_hash.

The resultant output is as expected :

Stock hash size : 5

Cheers!
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker