Wednesday, November 17, 2010

Setting a useful command prompt in Unix

I just came across a unix system at my workplace that had a static prompt set. Basically, the prompt was just the shell executable's name and version, more like 

bash-3.2 $

Well, this kind of prompt has many drawbacks, some of them i'd list here - 
1. You never know (just like that) where you are in the file system. When you are dealing with multi-directory situations, you might want to stop typing pwd to figure out ur current location.

2. You never know by what user you are logged in (again, just by looking at the prompt). You'd have to run a whoami to figure that out.

3. More importantly, if you are dealing with multiple systems, this one's the most killer.  You never know to what system you are logged in right now. you'd have to issue a hostname command.

Well, there might be, and for sure there are many other, consequences of having such a cryptic command prompt.  And therefore, my favourite, to have a command prompt, that displays at least these three things, always, dynamically...

Something like, 

raghav@deskubuntu:/homes/raghav/rails $ 

wherein, I am always aware of the three things mentioned earlier. This is very very useful when you are dealing with multiple systems and you have multiple users who are configured to run different types of processes.  For example, an oracle user who is supposed to be owner of oracle processes, and an informatica user which is supposed to own everything linked to informatica, and then a connect direct user which owns the CD processes, which receives files coming in from some other system.

With this kind of system, and your own user id to log in to the system, you'd better be careful which processes you are looking at / launching and by what user.  Its really very very important.

When and if you are dealing with a multiple system scenario, like dev / test / acceptance / production, you'd be better advised to use something like this only.

the magic command to do that is by setting appropriate flags and text in a environment variable called PS1.

Just set PS1 to your .profile or .bashrc (depending on your environment) file and you are set .

The example prompt that I mentioned can be achieved by saying - 

export PS1="\\u@\\h:\\w \\$ "

There are many more possibilities that go with special meanings for PS1 variable. Read some of them here - 

    * \$ : if the effective UID is 0, a #, otherwise a $
    * \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
    * \\ : a backslash
    * \] : end a sequence of non-printing characters
    * \a : an ASCII bell character (07)
    * \@ : the current time in 12-hour am/pm format
    * \A : the current time in 24-hour HH:MM format
    * \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
    * \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
    * \e : an ASCII escape character (033)
    * \H : the hostname
    * \h : the hostname up to the first '.'
    * \j : the number of jobs currently managed by the shell
    * \l : the basename of the shell’s terminal device name
    * \n : newline
    * \nnn : the character corresponding to the octal number nnn
    * \r : carriage return
    * \T : the current time in 12-hour HH:MM:SS format
    * \t : the current time in 24-hour HH:MM:SS format
    * \u : the username of the current user
    * \s : the name of the shell, the basename of $0 (the portion following the final slash)
    * \V : the release of bash, version + patch level (e.g., 2.00.0)
    * \v : the version of bash (e.g., 2.00)
    * \W : the basename of the current working directory
    * \w : the current working directory

Tuesday, November 16, 2010

Happy 2nd Birth Day ....

I am not there with him today, the mom is. And though he would have some fun for sure, I am going to miss all of that... :(

wish you a happy birthday remotely...

Monday, November 15, 2010

scripts and hash bang ( #! )

More often than not, people have to tell the unix shell / perl scripts or other programs where lies their interpretor, e.g. write their command line calls as 

perl SomeScript.pl

or 

ruby ARubyProgram.rb

or 

sh SomeShellScript.sh

this is because the system may not be aware of the location of the the executable interpreter of the exact type that needs to be used for the corresponding script.  Well, for this purpose, windows has the file extension association concept, but we are dealing with Unix like systems not windows, so that option is not really available to us (besides, there are ill effects of that convention too, but lets not go in that discussion).

So, to tell a unix program where to find its interpreter, besides launching the script along with it on command line, there is another way, and rather beautiful at that.

Just put the exact path of your interpreter executable at the very first line of your script preceded by these two magic characters, a hash and an exclamation (#!) also called as hash-bang or shebang.  Now, once your script is marked as executable (see chmod), you are good to go, no need of putting explicit calls to the interpreter to run your code.

Basically, your code should now look like this - 

#!/usr/local/bin/perl5
print "testing hashbang with raghav"

Save this short script as aa.pl (assuming that your system has perl 5 interpreter installed in the location I used). Make the script executable (chmod) and you can just launch the script, like ./aa.pl  instead of earlier example perl aa.pl

A word of caution though, these magic characters have to be absolutely the first and second character of the file, no exceptions to that. Else, the system cant make out the special meaning of this and the purpose is lost.

Pretty neat.. hunh...

Tuesday, November 9, 2010

Experience with Air India - 2

The return flight from Delhi to Bangalore, on the evening of Sunday 7th Nov 2010, turned out to be the most horrible flying experience I have ever had, till date.  The flight was AI-622, bound to fly out from T3 at 2010 hours 7th Nov.

When I reached at the airport, the check-in area was a mess in itself.  The Air India staff had invited all the passengers to check-in from all the counters, basically creating a mess of a situation. When they realized what they have done, they scrambled to a fix, and started calling out to the people for whom the flights were about to depart (and these ppl were still stuck in check-in queues).

When these people were taken away, my checkin process suddenly looked easier and faster.  Basically, I was all checked in at around 6:45 and inside the boarding gates area by that time.



The display panel had no mention of my flight but other flights both domestic and international, before and after my flight were listed properly, with whatever status applicable to them.  When inquired, they said, it was ok, not to worry, and told me of my gate no for boarding (also that the flight was on schedule).

By 7:25 pm, the boarding should have begun, but there was no air india official to be seen around the gate till that time, or even later, till 2000 hrs.  I think thats when I begun to think, that something was wrong.  I went to the info desk and asked the person there, he had no information.

Slowly people started assembling around the gate and asking questions. There was nobody to answer them. Slowly the mob mentality was becoming visible. People shouting, grabbing whoever was visible in an air india garb, or with some kind of airport office link.

Initially, someone caught hold of a lady, fairly senior (by age i mean), and started asking her questions. She DID NOT know anything. Someone said that she was the duty manager, and this angered people even more.  

By now, it was well past 2100 hrs and still nobody had appeared on the assigned gate.  Then people caught hold of two air india officials who were just passing by. The mob really got angry and wanted answers.  The two guys were so visibly helpless they couldnt do anything.  Ppl asked them to call up the responsible officials, but none of the officials were picking up their phones.  Then one passenger asked for the officials' number and called from his own mobile. The official did pick up the phone, but, still couldnt offer any answers to the situation.

Slowly a pattern was emerging out of the situation. No flights scheduled to leave after 1900 hrs or so were taking off. Flights to Bangalore, Chennai, Kolkata, Mumbai and Dubai were in this unknown situation. Some people for bangalore flight were waiting since 1100 in the day since their 1300 flight was cancelled and they were bumped up to this 2010 flight.  Effectively they were the worst hit guys, waiting for about 10 hrs already.

Finally, the mob started walking with this official, demanding to be put in touch with the officials face to face, nothing happened, since the passengers are not allowed to leave the boarding area after check-in. People were diverted to another official at another gate, who could not answer any questions anyway.

By now, it was already 2300 hrs, and still no answers. It was pretty chaotic, since nobody knew whats the status of the flight, and whats going to happen.  There was some infants among the passengers. Their parents were the more anxious lot. A Danish national got migraine attack during all this chaos. There were at least 10 people who were over 70 yrs old, and the waiting process was not helping them at all, let alone the uncertainity.
As it turned out, around 2330 there was announcement of the cancellation the bangalore flight.  It just broke some patience barriers. People started shouting at the info desk guy, since he had not been advised about the next steps for the passengers.  

He made a few phone calls and a lady appeared.  This lady tried to offer some hope, but it was a failed attempt. People had realized by now that air india has no intention of flying the people to their destinations and it would be a cancelled flight for good. They are just not announcing it in good time so that they wont have to pay the passengers' hotel stay etc.

They still had no clue around the solution to the problem.  Now a new face appeared, Mr Nalin. He claimed to be in an executive authority and wanted to fix the problem for good. He promised a special flight to Bangalore at 0145 hrs in the night. After making some other promises, he went off. Nowhere to be seen again.

People thought that they have been conned again.  By now, its already 0015.  Suddenly, people caught hold of two female officials in air india garb. The poor girls didnt know anything but were shouted at anyway.  A new face appears now, Mr Pankaj, supposedly GM of Air india.

He was shouted at all possible ways. Everybody was venting his anger on him, but guy showed remarkable patience and didnt react even once. We were thinking, had he reacted even once, god knows how the mob would have acted/reacted.  I would say, he was not physically handled, barring that, all possible ways were used to vent anger at him.

Finally, he started moving some things, the officials started appearing. He found a crew for one of the flights, and then another crew came in.  Some more officials came in, a lot of CISF people showed up. Apprantly to handle the situation if the mob went physical with him.

Already 0100 hrs and no sign of boarding activity. Mob had not let him go anywhere, so he announced that the bangalore flight will leave "if not 0145 perhaps by 0200 or 0215".  Another 30 minutes passed by, and now the mob was on the brink.  A gentleman, father of two young kids, literally charged at him, wanting to know when the flight would actually go.  

Now he says, 0300. And this was genuinely interesting, the guy asked Mr GM, what would you do if the flight does not depart by 3 AM... the GM guy had no answer, literally tense face, sweating brows, no voice. 

Finally, around 0255 there was an announcement for boarding for the bangalore flight.  A decent 7hrs 30 mins delay, and I am sure, had the people not been so vocal and demanding, the flight would never have been arranged and we got home the way we got home.

At the end, the flight, which should have landed bangalore 2240 the previous evening, now landed past 0600 the next morning, spoiling the next day for everybody, more so for the working passengers.

Someone rightly spoke, "this is my last flight with air india, never again". Well, I am with the group who says, "never say never", but for once, for this one I feel like agreeing with him. Never Again with air india, or at least till the wounds are fresh :)

Experience with Air India - 1

It was not my first time flying with air india, perhaps 2nd or 3rd, but its going to be the last one, at least these are the emotions rights now...

I booked this round trip in october last week, attempt to go home for Deepawali utilizing the long weekend.  It was a Bangalore - Delhi - Bangalore round trip, and I was pretty excited to have clinched this deal.

Flying out to Delhi from Bangalore was already giving indications of trouble with Air India.  When I reached airport, they had already cancelled my flight.  Later I got to know that they had in-fact merged my flight with another flight, which was an international flight, coming from somewhere. The fact was, almost nobody was certain whats the situation. On the check-in counter, I was told that my seat was not confirmed yet, was on standby. Luckily, it was confirmed within the next few seconds itself, and I had a seat (which I realized later, was the last available seat on the flight).

The flight was delayed by about half an hour was another fact. During the waiting period, I met a few passengers, who were waiting since 4-5 hours since another afternoon flight was delayed and the passengers were supposed to leave together on this flight.

Inside the aircraft, we were welcomed by two angry air hostesses, and a third one relatively better.  Mis-management was rampant there too. Ground staff had put in some stuff meant for passengers cabin luggage and the air hostesses were having hard time accomodating passengers' luggage.


Once I got to my seat, I found out a few more things. The hand rest of the seat was taped using cello tape, or else it would just come out into your hands. Later during the flight, one of the overhead lights came out into the hands of of the passengers when she was trying to refocus the beam.  All in all, the aircraft looked like from stone age,  not withstanding the fact that they didnt have aircrafts in stone age :) .

Eventually, we reached delhi airport, but were informed that we would land on the newly built-operationalized terminal (known as T3).  While going to the baggage claim area through a bus, we could actually see labour working here and there. Basically, it was evident that construction work is not over.

The baggage claim belt didnt start until about half an hour after our arrival in the lounge. Some passengers had literally shouted out to get some air india officials around there and take some action for that.

At the end, it turned out, the new terminal is really still new for the people also, and they have issues synchronizing everything. 

It turned out that the whole ordeal took about 2 hours extra than the normal.