Tuesday, April 27, 2010

How to find the length of a variable?

    In UNIX, one deals with variables all the times. Many a time, you might want to find the length of a variable. The length of a variable can be found or calculated using many different ways in UNIX. Lets explore the different ways to do it:

1. In the k-shell or bourne shell, the simple echo command can be used to find the length of a variable.
#VAR="welcome"
#echo ${#VAR}
7
#

2. The echo command with wc can also be used to find the length of a variable.
#VAR="welcome"
#echo -n $VAR | wc -c
7
#

3. The printf command with wc can also be used to calculate the length of a variable.
#VAR="welcome"
#printf $VAR | wc -c
7
#

4. The expr command can also be used to find the length of a variable.
#VAR="welcome'
#expr $VAR : '.*'
7
#

5. The awk command can also be used to calculate the length of the variable.

#VAR="welcome"
#echo $VAR | awk '{print length ;}'
7
#

6. The perl command can also be used for the same:

#VAR="welcome"
#echo $VAR | perl -ne 'chop; print length($_) . "\n";'
7
#

    All the above examples all common across all UNIX flavors such as Solaris, HP-UX, Linux, AIX, etc.

[Note: In the above examples, the setting of variable VAR is shown with respect to k-shell/bourne shell. Depending on your shell, the setting of VAR variable will change.]

Friday, April 23, 2010

rlogin: How to login to a UNIX account without password?

       Every UNIX user logs into a user account by giving the username and password. There are situations when a user has to login to a particular account many times during the day. It would have been easier had the user not been asked to provide the password every time he tried to log-in.

     There are many ways in which a user can login to an account without password. In this article, we are going to see how to use the rlogin command to achieve this.

    The basic use of rlogin command is to do a remote login. Though the name tells 'remote login',the rlogin command can be used to login  to a user account of the same machine or a different machine.


Using rlogin to login to a user account in different box:

    Let us consider a scenario. A user 'gpr' in UNIX box 'SandalWood' tries to login to the user 'blogger' in UNIX box 'TeakWood'.

  1. In the home directory of the user 'blogger' in 'TeakWood', create a file .rhosts if not present and add the following contents to it.

#cat $HOME/.rhosts
SandalWood gpr
#

  2.  The user 'gpr' should now execute the rlogin command from 'SandalWood' box as shown below:

#rlogin TeakWood -l blogger
#

   When  issuing the above rlogin command, the user will now be directly taken into the user account 'gpr' without being prompted for password.

     Whenever the rlogin command is used, it first checks for .rhosts file in the destination user account's home directory. On finding the .rhosts file, it tries to find the entry for the remote user being logged. If a valid entry exists, the user gets considered as a trusted user and hence not prompted for password.

Using rlogin to login to a user account in the same box:
        
  If the rlogin command is used to login to a user account of the same machine, the hostname need not be specified in the .rhosts file. In place of hostname, the symbol '+' can also be used as shown in the below example: 
       
#hostname 
TeakWood
#echo $USER
gpr
#cat $HOME/.rhosts
+  blogger
#

     The above entry in the .rhosts file will allow the blogger user 'blogger' to login to the user account 'gpr' without password.

Tuesday, April 13, 2010

What is the difference between export, set and setenv UNIX commands?

  export, set and setenv commands are used in UNIX for setting value of a variable or an environment variable. In order to understand the difference between the set, setenv and export UNIX commands, the user should know the difference between a normal variable and an environment variable.

   Let us  consider an example. In k-shell or bourne shell, a variable is defined as shown below:

# FILE=”output.txt”

     This means the variable FILE is assigned a value 'output.txt'. This value can be checked by doing "echo $FILE".  This FILE variable is a normal or local variable.  This assignment makes the scope of this variable  only inside the shell in which it is defined.  Any shell or a process invoked from the original shell will not have the variable FILE defined as shown below.

      
#FILE=”output.txt”
#echo $FILE
output.txt
#ksh
#echo $FILE

#

     There are instances or situations where we would like to define a variable and it should be accessed in all the shells or processes invoked by the original shell. This can be achieved by the export command in ksh/sh as shown below.
      
#export FILE=”output.txt”
#echo $FILE
output.txt
#ksh
#echo $FILE
output.txt
#

       This FILE variable is now an environment variable. An environment variable is the one which can be accessed across all the shells or processes initiated from the original shell of the environment. So, in ksh/sh, a variable can be made an environment variable using the export command.

       set and setenv are the c-shell/tc-shell alternatives for setting a local variable and environment variable respectively. The set command is used for setting local variable, setenv is uesd for setting an environment variable:

   The example below shows the set command usage:
       
#set  FILE=”output.txt”
#echo $FILE
output.txt
#tcsh
#echo $FILE

#

   The example below shows the setenv command usage:
          
#setenv  FILE ”output.txt”
#echo $FILE
output.txt
#tcsh
#echo $FILE
output.txt
#




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