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)

HP:

1. Mail with body:
    #mailx -m -s "report" gpr@xyz.com < usage_report

      where usage_report is the file containing the body to be mailed, and "report" is the subject of the mail and gpr@xyz.com 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" gpr@xyz.com

      where usage_report is the file to be sent as attachment.



SunOs:

1. Mail with body:
    #mailx  -s "report" gpr@xyz.com < usage_report

      where usage_report is the file containing the body to be mailed, and "report" is the subject of the mail and gpr@xyz.com 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" gpr@xyz.com

      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
/bin:/usr/bin

    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.

[XXX:~gpr]#PATH=$PATH:/usr/bin

      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
[XXX#~gpr]

    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.

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

    map  #3 :se nu


HP-UX:
  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.