Facing problem with Alias created through script.

Hi Guru's,

I am creating alias for db instance running on a server through script, am able to create them based on /etc/oratab entries and can use successfully with the below script.

#!/bin/bash

SCRIPT_PATH=${HOME}/scripts/db/script
LOG_FILE=${HOME}/scripts/db/log/database_instances_alias.log
CONN_DATABASE='sqlplus -s / as sysdba'
DB_INSTANCES=$(cat /etc/oratab | egrep -v '^#|^$' | grep -v '^+'|cut -d":" -f1,2)
sed -n '1,8p' ${HOME}/.bashrc > ${HOME}/.bashrc_bkp
echo " " >>${HOME}/.bashrc_bkp
printf "%s%20s%20s%20s%20s\n" "S.No" "DATABASENAME"  "ORACLE_HOME"  "DB_STATUS" "DB_MODE" > ${LOG_FILE}
SERIAL=0
for SINGLE_INSTANCE in ${DB_INSTANCES[@]}
do
##DATABASE NAMES
DATABASE_NAME=$(echo $SINGLE_INSTANCE | awk -F ":" '{print $1}')
##ORACLE HOME
DATABASE_HOME=$(echo $SINGLE_INSTANCE | awk -F ":" '{print $2}')
ps -ef | grep pmon | grep -q $DATABASE_NAME
  if [ $? -eq 0 ]; then
    DB_STATUS="UP"
    export ORACLE_SID=$DATABASE_NAME
    export ORACLE_HOME=$DATABASE_HOME
    DATABASE_MODE=$(echo "select open_mode from v\$database;" | $CONN_DATABASE | sed -n '4p')
       case $DATABASE_MODE in
             'MOUNT')
                                DATABASE_MODE_S=${DATABASE_MODE}
             ;;
             'READ WRITE')
                                DATABASE_MODE_S=${DATABASE_MODE}
             ;;
             'READ ONLY')
                                DATABASE_MODE_S=${DATABASE_MODE}
             ;;
             'NOMOUNT')
                                DATABASE_MODE_S='Instance Running Not Mounted'
             ;;
             *)
                                DATABASE_MODE_S='N/A'
             ;;
       esac

  else
    DB_STATUS="DOWN"
    DATABASE_MODE_S="N/A"
  fi
SERIAL=$(expr $SERIAL + 1)
printf "%s%20s%25s%15s%20s\n" "$SERIAL" "$DATABASE_NAME"  "$DATABASE_HOME"  "$DB_STATUS" "$DATABASE_MODE_S" >> ${LOG_FILE}
echo "alias $SERIAL='export ORACLE_SID="$DATABASE_NAME";export ORACLE_HOME="$DATABASE_HOME";PS1=\"[\\u\\h ] $DATABASE_NAME \$\"'" >> ${HOME}/.bashrc_bkp
echo "alias showdb='$SCRIPT_PATH/database_instances_alias_bkp.sh;cat $LOG_FILE;. $HOME/.bashrc'" >>${HOME}/.bashrc_bkp
done
mv -f $HOME/.bashrc_bkp $HOME/.bashrc
. $HOME/.bashrc

But i am facing problem when the number of entries changed
like
suppose there are 8 entries in oratab and the script will create 8 aliasis, if i delete 2 entries from oratab so script will create alias for 6 entries only, but am not able to unalias 7 and 8. so how can i unalias them

someone please help

Regard's
Venkat

If you know the alias count upfront, why don't you

for i in {1..8}; do unalias $i; done

as the first statement in the script?

Hi,

Thanks for the response RudiC,

yes, may someone have better idea around
when am checking length of an array it displays 1
echo ${DB_INSTANCES[*]}
so am following echo ${DB_INSTANCES[*]}|wc -l
is this a correct way to follow.

please help

Regard's
Venkat

---------- Post updated at 06:37 PM ---------- Previous update was at 05:15 PM ----------

Hi All,

Thank you RudiC driving me

I resolved my issue with snippet below

diff $HOME/.bashrc $HOME/temp_alias_compare_file >/dev/null 2>&1
  if [ $? -eq 1 ];then
    diff $HOME/.bashrc $HOME/temp_alias_compare_file | grep 'alias' | grep -v 'showdb' | while read line
         do
            unalias $(echo $line | awk -F"=" '{print $1}' | awk -F"alias" '{print $2}' | sed 's/^ //g')
         done
  fi
. $HOME/.bashrc

Regard's
Venkat

Please use code tags as required by forum rules!

As you are using awk anyhow in your solution, why not make the entire action an awk script?
Two thoughts:

  • when creating the aliases in the .bashrc script, why not add the respective unalias es in parallel?
  • as you are using bash , try unalias {1..20} and just accept/ignore the not found errors...
1 Like