Sunday, January 31, 2010

Perl CGI programming in windows

  Perl CGI programming can be done in Windows/UNIX. The below instructions tells about the CGI perl programming in windows.  
   Before trying CGI, we need to have the set up necessary for the CGI programming.  The following are the things needed for it:
1. Active Perl Installation
2. Apache web server
    With Active Perl, once can work with all the command line Perl programs. However, for the CGI programming, we need the Apache web server(xampp). Download the latest version of xampp and install it. Both these are easily available on Internet.

  Once the Apache (xampp) intallation is done, click on the link, http://localhost/xampp/  , to confirm whether the installation is sucessful or not. On clicking this link, the status of the Apache can be known. If an error exists, the installation is not successful.

   Once done, create a folder "cgi-bin" in the "xampp" directory. xampp directory will be in the place in which you chose the installation of xampp to be done.  Create a sample cgi program and place it inside the "cgi-bin" folder.

 Let  us see the example of a sample 'Hello World' program. The following are the contents of the program:

#!C:\perl\bin\perl.exe -wT
print "Content-type:text/html\n\n";
print "hello world";

[Note: The first line indicates the path of the Perl exe. Depending on your path, modify it accordingly].

 Save the above snipped as test.cgi  .Open the Internet explorer, and type  "http://localhost/cgi-bin/test.cgi".

  You should see the 'hello world' text on your browser screen.

Sunday, January 17, 2010

Mailing from command line in UNIX

      There are lot of instances when we want to send a mail to our outlook or personal account from the UNIX account.  Say, could be to send a mail with a subject alone, mail with contents or mail with attachment.  The mail with attachment is one of the most used forms among the three because many a time we would need to do something with a UNIX file in windows. So, instead of FTPing which takes some time, we can easily mail the file being from the UNIX account.

   The following section shows how to send mails from the UNIX box (HP and SUN)


1. Mail with body:
    #mailx -m -s "report" < usage_report

      where usage_report is the file containing the body to be mailed, and "report" is the subject of the mail and is the mail-id of the user to whom the mail is to be sent.

2. Mail with attachment:
    #ux2dos usage_report | uuencode usage_report  | mailx -m -s "report"

      where usage_report is the file to be sent as attachment.


1. Mail with body:
    #mailx  -s "report" < usage_report

      where usage_report is the file containing the body to be mailed, and "report" is the subject of the mail and is the mail-id of the user to whom the mail is to be sent.

2. Mail with attachment:
    #unix2dos usage_report | uuencode usage_report  | mailx  -s "report"

      where usage_report is the file to be sent as attachment.

Saturday, January 16, 2010

What is a PATH variable?

        PATH variable is one of the most important concepts of any operating system.  PATH variable tells the places from where the Operating System(OS) will search for whenever a command is given. Lets explain it a bit in detail.

   Say, we fire the ls command. Once the ls command is executed, all the files and directories in the current directory are displayed.  What happened now in order for the OS to display the files and directories?

   When the ls command is executed, the OS starts searching for an executable named ls.  Where does it search? It searches in all the directories mentioned in the PATH variable. The moment it finds the executable, it executes the command the the output is displayed.

For example:

[XXX : ~gpr]# echo $PATH

    The above PATH variable tells that whenever a command is given, the OS will search for the command in the /bin directory first, followed by the /usr/bin directory.

  Sometimes, when we give a well known command, we get the 'command not found' error. This error occurs usually whenever the PATH variable is corrupted.  When the OS started searching for the ls command, it did not find the executable in any of the directories defined in the PATH variable and hence the error 'command not found'.

   This can be corrected by updating the PATH variable with the /usr/bin directory.


      The above expression states to append the /usr/bin directory to the existing PATH variable.

Make a shell script work like a UNIX command

        Whenever we write a shell script, we run the shell script by using the syntax ./first, assuming first is the name of the shell script. In this way, the user always has to be in the directory containing the script in order to run this. If the user is in a different directory, the user has to give the full path to run the command ~xxx/abc/first. 

    Now, as the number of scripts keeps increasing, it becomes tougher to run the script in this way, moreover if they are kept in different directories. Let's see how we can run this script like any other UNIX command.

The following are the steps:

1.  Create a directory bin under the home directory.
2.  Place the shell script file in the bin directory.
3.  Update the PATH variable.
        PATH=$PATH:~gpr/bin  (assuming gpr is the username)

   The content in the file first  is as shown below:

[XXX# ~gpr/bin]# cat first
echo "Welcome to shell scripting"
[XXX# ~gpr/bin]#

  Once this setting is done, you can use the first script being in any directory in the account, like any other user command as shown below:

[XXX# ~gpr]# first
Welcome to shell scripting

    From now on, any new script created needs to be put in the bin directory. As soon as it is put, it can be used like any other unix command.

  In case other members of the group wants to use these scripts, they need to do the Step 3 alone in their user account.


Sunday, January 10, 2010

Mapping Function keys in vi

  UNIX allows us to map functional keys like F1, F2 to  some commands. .exrc file is used in order to make these mapping permanent.

   For example, say everytime we open a file, when we want to see the file contents with line numbers, we go to the escape mode and type :se nu.  Instead, we can have a short cut for this.

  Let us see how to map the key F3 to do set line number inside vi.

  In the Sun OS, this can be done by having the following entry in the .exrc file:

    map  #3 :se nu

  In the HP machine, it is a bit different because the key codes are read differently.  Have the following entry in your .exrc file:

   map ^[[13~  :se nu

   Note: The above map pattern is actually typed as shown below:
    ^[[11~ = Ctrl-V + Esc + [ + 1 + 3 + ~

   Once this setting is done, open a file in vi, and press F3 and enter, you should see the line numbering done.

Important tips in VI editor

  • Reading file into vi:  When we are editing a file in vi, it may so happen we may need to copy the contents of a file into this file. VI provides a way for this using the 'r' command.  Say, we are editing a file f1, and in the 10th line of f1 we want to paste all the contents of file f2. Open the file f1, move the cursor to the 10th line, and give the following command in the escape mode:   
               :r f2    

  • Writing portion of a file to a different file:  When we are editing a file in vi, we may want to write a set of lines in a fresh file. Say, we want to write lines 10 to 20 of the current file f1 to a new file f2. Open the file f1, and give the following command in the escape mode:
               :10,20w f2

          • Set tags in vi:   For programmers,  when we open a file, in order to go to a function definition, we need to close the file, search for the file of the function definition and open the definition file. In places where there are lots of function, this is very cumbersome. Tags can be highly useful here. Once we generate the tags for our function calls, we can set the tags variable in the vi editor, after which on clicking some hot key combination, we can reach the function definition, and similarly with some other hot key combination, we can return to the original file. Issue the following command from the escape mode,
            •             :set tags=tag file
            • Abbreviations in VI: While editing in vi, we may use a particular word lot of times, say subscriber.  And assuming we use this word lot of times, it will be quite time consuming to type this word every time. vi allows us to use abbreviations by which we can have an abbreviation for the subscriber_no word. Also, the abbreviations used are also applied for searching also. Say, if we use sub as the abbreviation for subscriber_no, every time we type sub, it will be automatically be replaced with subscriber_no. To set the abbreviation, go to the escape mode and type:
                    :set sub subscriber_no
              • Swap characters in VI: While editing in vi, lot of times we make spelling mistakes. For example, we may write 'the' as teh'.  In order to correct it, we take the cursor to 'e' and type x command which removes 'e'. And then we go to append mode and type 'e' so it becomes 'the'.  This can easily be achieved using the 'xp' command combination.  x deletes the character and p pastes the character to the right of the character at the cursor. Hence, when 'xp' is issued with cursor at 'e' in 'teh', it becomes 'the'.

              • Mapping keys: vi allows us to map keys to some commands. For example, when we want to quit a file, we issue the command :q!.  We can map this say, to say Q, which means whenever we give 'Q' command, it works like :q!. Once this mapping is done, whenever we give 'Q' in escape mode, it will quit the file.
                          :map  Q :q!