Monday, 7 March 2016

Unix - Scripting Useful commands

Basic Steps to Create Host Concurrent Program 1. Create a ".prog" File
 2. Move the File to Server in any Custom TOP "bin" Folder (Tools Winscp / Filezilla)
 3. Execute the Below commands oderly maner
     3.1 Putty> cd $XXCUSTOM_TOP/bin
         -- Here the Custom TOP is the File Location TOP 
     3.2 Putty> dos2unix sample.prog
     3.3 chmod 755 sample.prog
     3.4 Putty> ln -s $FND_TOP/bin/fndcpesr sample
          -- Here the File name with out .prog Extension
          -- We are creating the Softlink to our File
     3.5 Add continue to create concurrent program Excutable and Program but the Executable type is "HOST"


Sample Commands:

1. Commenting any line in UNIX by using hash(#)
   # this is commented
2. Getting the Oracle Standard Parameters
   #user/bin/ksh
   ORAUSER_PASSWD=$1
   USER_ID=$2
   USER_NAME=$3
   REQUEST_ID=$4
 
3. Below command is used to print to Log File
   echo "Hi"
  
4. In Unix we can create Variables directly when evere we need and we can run any sql/plsql script directly
   like a block
 
Unixvariable =  `sqlplus -S  ${ORAUSER_PASSWD}<< ENDOFSQL
    set feedback off
    set serveroutput on
    exec APPS.Package_name.procedure_name(parameters);
    exit
    ENDOFSQL`
echo "${Unixvariable}"

5. I have text containg three value seperated by comma(,)
   -- Unixvariable = '123,234,456'
   i want to store every value into different variable

value1 = `echo $Unixvariable | awk -F"," '{print $1}'`
value2 = `echo $Unixvariable | awk -F"," '{print $2}'`
value3 = `echo $Unixvariable | awk -F"," '{print $3}'`

6. if there is any spaces in text we can remove
 newvalue1 = `echo $value1 | sed -e 's# ##g'`

7. UNIX is power full we can track each and every step, below function works like SQL%NOTFOUND in Oracle
    here i have written script for connecting to SFTP Server, if the connection is success no issue, if connection is not established due to any problem we can track this failure by using  below command
if [ ${?} -ne 0 ]

it check most recent command is executed successfully / not.

connection=`sftp "${vendor_username}"@"${vendor_host}" << END
    cd "${Direcoty Address}"
    lcd "$CUSTOM_TOP/bin"
    GET *.csv
    quit
    END`
#Check Log File Exist Before Validation Start
if [ ${?} -ne 0 ]
then
    echo "Unable to connect to destination server."
echo "${connection}"
    exit 2
    fi

8. We can call the SQL*LOADER Programs from UNIX Script easily

   CONTROL_STATUS=`sqlldr userid=${ORAUSER_PASSWD}            control=$CUSTOM_TOP/bin/control_file.ctl data=datafile.csv log=$APPLCSF/log/datafile.log bad=$APPLCSF/log/datafile.bad<< ENDOFSQLLDR
   ENDOFSQLLDR`
   echo "${CONTROL_STATUS}"

9. We can capture the date into variable

   currdate = `date +%d_%m_%g_%H_%M_%S`

10. We can create Files into the server using unix script in an easy manner
    `sqlplus -S ${ORAUSER_PASSWD} >> Kranthi.csv <<EOF
set head off;
set pages 0;
set linesize 2000;
set feedback off;
SELECT  'Emp Number'||','||
   'Emp Name' ||','||
     'Emp Sal'
from dual
/

exit
EOF`

11. Move the All ".csv" Files from Source Directory to Destination Directory
    cd "Source_Direcoty"
    find ./ -type f -name "*.csv" -print | xargs -l56 -i mv -f {} "Destination_Directory"

12. We can create Functions and we can pass parameters to Functions, here i am passing one parameter to Function
    Delete_Files()
    {
    cd $1
    echo "${PWD}"
    fileArray=($(find *.csv -mtime +10))
    filescount=${#fileArray[@]}
    for (( i=0; i<${filescount}; i++ ));
    do
    echo "${fileArray[$i]}"
    rm -f ${fileArray[$i]}
    echo "File deleted from Folder"=${fileArray[$i]}
    done
   }
   echo "Searching Files in bin direcory older than 10 Days...."
   Delete_Files $CUSTOM_TOP/bin

13. We can apply file permission(777) to all subfolders by using this command
    chmod -R 777 *
 
      
15. Control Structres in UNIX
 
     if test condition
      then
      statements;
fi

if [condition];
then
     statements;
     else
     statements;
     fi

if [condition];
then
   statements;
     elif
     then
     statements;
else
      statements;
     fi


     for expression
     do
  statements;
     done

16. File Comparisions
    -eq equal to
-ne not equal to
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to

No comments:

Post a Comment

Useful fusion query for User access

  Table FUN_USER_ROLE_DATA_ASGNMNTS is used for “Manage data access for users”. it will store use and role assignment to data security. Tabl...