Wednesday, May 23, 2018

Shell Script - Add Entry (TNSNAMES.ORA)

Hi, everyone.

Please find below a shell script that can be used to add an entry in the tnsnames.ora:


  • add_tnsnames_entry.sh:
#!/bin/bash

####################### ADD AN ENTRY IN THE TNSNAMES #######################
### 
### add_tnsnames_entry.sh
### PARAMETERS ###
### $1 TNS ENTRY NAME - [ENTRY_NAME]
### $2 IP OR DNS OF THE TARGET DATABASE - [HOST_ADDR]
### $3 PORT NUMBER TO CONNECT TO THE TARGET DATABASE - [PORT_NUM]
### $4 SERVICE NAME OF THE TARGET DATABASE - [SRV_NAME]
### $5 DIRECTORY OF THE TNSNAMES.ORA - [TNS_ADMIN_DIR]
### 
####################### ADD AN ENTRY IN THE TNSNAMES #######################

export ORACLE_HOME=/opt/oracle/product/12.1.0/db
export TNSNAMES_FILE=tnsnames.ora
export ENTRY_NAME=$1
export HOST_ADDR=$2
export PORT_NUM=$3
export SRV_NAME=$4
export TNS_ADMIN_DIR=$5


if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ]; then

  echo "Missing one or more required parameters: [ENTRY_NAME] [HOST_ADDR] [PORT_NUM] [SERVICE_NAME] [TNS_ADMIN_DIR]"
  exit;

fi


chk_entry=`cat $TNS_ADMIN_DIR/$TNSNAMES_FILE | grep "$ENTRY_NAME =" | wc -l`

if [ -n "$chk_entry" ] && [ "$chk_entry" = 0 ]; then

  echo "Creating the required entry..."
  echo ""

echo "
$ENTRY_NAME =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = $HOST_ADDR)(PORT = $PORT_NUM))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = $SRV_NAME)
    )
  )
" >> $TNS_ADMIN_DIR/$TNSNAMES_FILE

   elif [ "$chk_entry" != 0 ]; then

   echo ""
echo "This entry already exists: [$ENTRY_NAME]."
echo "Nothing to do!"
echo ""
   exit;
 
fi 

################################################################################

  • chmod +x add_tnsnames_entry.sh

e.g:

./add_tnsnames_entry.sh CDB12C rac1-vip 1521 CDB12C1 /opt/oracle/product/12.1.0/db/network/admin

No comments:

Post a Comment

Pages