Wednesday, April 7, 2010

How to do non-interactive FTP in Unix?

   FTP is one of the means used to transfer files from one system to another, either way. FTPing files is one of the routine tasks for a developer or a system administrator.  There could be instances when an individual's task involve FTP to be done quite frequently.


  Every time when FTP is done, one needs to login to the destination system using the destination user  credentials, and do the file transfer. This is called interactive FTP. This process is pretty time consuming. Non-interactive FTP, as the name suggests, is a process in which the user has minimal interaction with the system to do FTP.  The user credentials and the files information are used in an non-interactive manner in this method.

   Let us consider a scenario: A file named 'alpha.c' needs to the put to the UNIX box 'TeakWood' under the user 'blogger'.

   The following steps describe the non-interactive FTP steps to do the file transfer:

   1. Create a file named .netrc in the home directory.

#touch .netrc

   2. Add the following contents in the .netrc file:

       Syntax:

machine <hostname> login <username> password <password>
macdef init
prompt
put <filename>
bye


          where 'hostname' is the hostname of the destination machine
                    'username' is the user name of the destination machine
                    'password' is the password of the destination user.

       Actual:

machine TeakWood login blogger password abcd123
macdef init
prompt
put  alpha.c
bye


          The file .netrc is saved in the home directory with the above contents .

   3. Change the permission of the file to read-only to the user and none to the group and others.


#chmod 400 .netrc

    4.Execute the following command from the command prompt to do FTP:

#echo “quit” | ftp –v TeakWood

      The file got FTP'ed successfully to the destination box TeakWood using non-interactive FTP.  In our future articles, we will see how to write a script to automate the non-interactive FTP.

Sunday, April 4, 2010

How to connect to sqlplus from Shell?

   Sqlplus is an Oracle command line utility which is used to execute SQL and PL/SQL commands.  Connecting to sqlplus from UNIX box to retrieve data is one of the very common tasks and hence sqlplus becomes an important tool in shell scripting.  The data to be retrieved from Oracle could simply be a column value from a table or it could be set of data from more than one  table. This article explains the different ways to connect to sqlplus from shell and retrieve data.

   Let us consider the example of retrieving data from the EMPLOYEE table to get the employee-id for a given employee name:

 Example 1:
    This example shows a sample program which connects to the sqlplus and retrieve the employee-id for the given employee name.


#!/usr/bin/ksh

emp_id=`sqlplus –s $USER/$PASSW@$INST   << EOF
                set pagesize 0
                set feedback off
                set verify off
                set heading off
                select EMP_ID from EMPLOYEE where EMP_NAME='Blogger';
                exit;
EOF`
echo $emp_id



Example 2:
   This example explains the same as above, except this uses a variable name as a parameter.

#!/usr/bin/ksh

EMP="Blogger"
emp_id=`sqlplus –s $USER/$PASSW@$INST   << EOF
                set pagesize 0
                set feedback off
                set verify off
                set heading off
                select EMP_ID from EMPLOYEE where EMP_NAME='$EMP';
                exit;
EOF`
echo $emp_id



Example 3:
      This example shows another way to connect to sqlplus from shell.

#!/usr/bin/ksh

EMP="Blogger"
emp_id=`echo "
                set pagesize 0
                set feedback off
                set verify off
                set heading off
                select EMP_ID from EMPLOYEE where EMP_NAME='$EMP';
                exit;" | sqlplus –s $USER/$PASSW@$INST  `
echo $emp_id



Example 4:
  This example shows sql file based approach to connect to the sqlplus. The sql code is written in a separate sqlplus script file emp.sql, and the shell script emp_id.sh invokes the sqlplus script :

#cat emp.sql
set pagesize 0
set feedback off
set verify off
set heading off
select EMP_ID from EMPLOYEE where EMP_NAME='&1';
exit;

#cat emp_id.sh
#!/usr/bin/ksh
EMP="Blogger"
emp_id=`sqlplus –s $USER/$PASSW@$INST  @emp.sql $EMP`
echo $emp_id




Wednesday, March 31, 2010

What is crontab?

   UNIX operating systems contain a daemon called 'cron'. The job of the cron daemon is to wake up every minute and execute all the tasks scheduled for the particular minute.  The tasks to be executed by the cron are present in a file called 'crontab' file.

   The crontab file is present for every user in the UNIX box.  Generally, shell scripts to be executed on a timely basis is scheduled as part of the crontab. However, even standalone UNIX commands can also be scheduled as part of the crontab.

  UNIX has a command called 'crontab'. This crontab command can be used to create new crontab tasks and also to check the tasks scheduled in cron.

crontab usage:

  1. To check the crontab activities scheduled,

    #crontab -l
    4 5 * * sat     echo "Execute at 4 after 5 every saturday"
    #

    The above displayed task will execute at 4hours 5 mins on every saturday.

  2. To create a new task in crontab;

   #crontab -e
    20 00 * * *  tcsh ~gpr/bin/create_ctags v94_0
   #

    On typing the command 'crontab -e', a vi like editor opens up. We need to edit the file and put the above shown contents as editing any file in vi. And finally, save and quit the file. The above command creates a cron job to execute at 20hours everyday.

  Syntax of the crontab file:

   hh mm dom mon dow  

    hh   - hour   (0-24)
    mm - minute (0-59)
    dom - day of the month   (1-31)
    mon - month  (0-12)
    dow  - day of the week (0-6   0-Sunday  6-Saturday. Also, we can use the names, like sun, mon)

  The following are the important things to keep in mind:

  1. While testing cron activities, dont schedule any activity in the immediate next minute. Schedule atleast 2mins later. At times, cron does not refresh properly.

  2. When cron executes any crontab activity, it spawns a new shell and executes. This new shell does not inherit all the environment variables of the user. Instead, very few environment variables are set. The following are few of them: SHELL(/bin/sh), USER, LOGNAME and HOME

3. Because of the above reason, a shell script running successfully at the command prompt may not run successfully when scheduled in crontab. In order to overcome this, the user has to include the sourcing of his .login or .profile file inside the shell script.

Saturday, March 27, 2010

What is Shell Scripting?

  Shell script is nothing but a batch file of windows.  In layman's term, a set of UNIX commands put together in a file becomes a shell script. In addition, the shell also provides additional programming  logic handling thereby making it more useful.


  Shell scripting or programming is typically used when we want to automate tasks which we do repeatedly. Once a shell script is created for a task or activity, just the script needs to be run in order to perform the task from next time onwards. Undoubtedly, shell script saves lot of time for the end user.


  A good Shell programmer is one who has a very good grip on the UNIX command line interface. A Shell programmer with not a good knowledge of UNIX command line will end up writing big scripts, which actually could have been done using some UNIX commands itself.

Let's  see how to write a simple shell script to display the 'Hello World' :

1. Write a script file hello.sh with the following contents as shown below:

    # cat hello.sh
       #!/usr/bin/ksh
       echo "Hello World"
    #

 2.  Give executable permission to the file:

   #chmod u+x hello.sh

 3. Run the script:

   #./hello.sh
   Hello World
   #


The following things are to be noted:

1. The first line '/usr/bin/ksh' tells in which shell this script should be run.
2. This line is not mandatory. If this line is not present, the script will be run by the default shell of the user.
3. Every script should be given the executable permission in order to execute it.
4. ./hello.world tells the shell that the script 'hello.world' to be executed is present in the current directory.

 'Happy scripting'.

Sunday, March 7, 2010

VIM Installation

 This article tells you about the steps to install VIM in a UNIX box.  VIM is a very powerful tool when it comes to file editing. VIM works on top of vi. It has hundreds of features which will be handy for a everybody working in unix. There are hundreds of vim plugins available as well.  The following steps tells us the steps to install vim.

1. Download the latest vim version from the vim.org website: http://www.vim.org/download.php

2.  Unzip the vim file:
    #gunzip vim-7.2.tar.gz


   The file vim-7.2.tar got created.

3. Untar the tar file created in the previous step:
   #tar -xvf vim-72.tar

   A directory vim-72 would have got created.  Do a cd into the vim-72 directory.

4. Run the configure file .
    #./configure


    A make file would have got created.

5.  Before running the make file, ensure from the command prompt the variables $CC, $CFLGAGS and $LDFLAGS are unset.

    #make


  An exe with the name vim would have got created.

6.  Update the PATH variable to add the path where the vim exe is located.