Facing problem with Alias created through script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Facing problem with Alias created through script.
# 1  
Old 10-31-2015
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.

Code:
#!/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
# 2  
Old 10-31-2015
If you know the alias count upfront, why don't you
Code:
for i in {1..8}; do unalias $i; done

as the first statement in the script?
# 3  
Old 10-31-2015
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

Code:
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

Last edited by Don Cragun; 10-31-2015 at 04:12 PM.. Reason: Add CODE tags.
# 4  
Old 10-31-2015
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 unaliases in parallel?
- as you are using bash, try unalias {1..20} and just accept/ignore the not found errors...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Facing problem in the sqlldr & shell script

Guys i am facing two problems : (1) when i create the sql loader file the date format i m getting is this 28-DEC-11 12.03.14.107137 AM; for this i m using this script but unable to load the files trailing nullcols ( SERIALNO, AMOUNT, CLASS, MDN, VDATE "to_date(:TIMESTAMP, 'DD-MON-YY... (6 Replies)
Discussion started by: xal_kaushi
6 Replies

2. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

3. Shell Programming and Scripting

created a .alias, but they won't take (ksh)

I login with a common username, and have no access to the different login scripts for my personalizations. So for a while, I would hobble on over to a directory I made for myself, and then bring in my alias file where I would fix my backspace, create a few aliases, etc.: $ ./.alias $ Now... (4 Replies)
Discussion started by: hindesite
4 Replies

4. Solaris

Facing Problem with metaset in SVM

hi all, i am using solaris 5.10 on sun blade 150 and i am trying to configure diskset in sun volume manager. When i fire the following command, it says some rpc related error. bash-3.00# metaset -s kingston -a -h u15_9 metaset: u15_9: metad client create: RPC: Program not registered how to... (4 Replies)
Discussion started by: kingston
4 Replies

5. Shell Programming and Scripting

problem facing in if -else condition

can u plz tell me where is the error echo enter the filename to be searched read fname if #-d $fname then echo file exists if then echo itsa directory elif then echo its readable cat $fname else echo its not readable fi else ... (1 Reply)
Discussion started by: gotam
1 Replies

6. Solaris

Facing problem with zone

i am using this way to create zone1 and zone2 bash-2.05b# zonecfg -z zone1 zone1: No such zone configured Use 'create' to begin configuring a new zone. zonecfg:zone1> create zonecfg:zone1> set zonepath=/zone/1 zonecfg:zone1> set autoboot=true zonecfg:zone1> add net zonecfg:zone1:net>... (6 Replies)
Discussion started by: coxmanchester
6 Replies

7. Shell Programming and Scripting

facing problem in starting a process in background using shell script.

hey all, i am working on sun solaris machine and i want to start a process in background using shell script (actually i wanna start tomcat server using shell script). please dont tell me that append a & at last because this is not working in the shell script. i have also used nohup and... (8 Replies)
Discussion started by: dtomar
8 Replies

8. Solaris

please help as i am facing problem with uptime

Hi I am getting the uptime output as follows 12:40am up 4 day(s), 18:29, 2 users, load average: 38.97, 36.54, 34.89 The load average is too high . I have checked the processes , but no process is taking too much cpu time Please help (3 Replies)
Discussion started by: guy009
3 Replies

9. UNIX for Dummies Questions & Answers

facing a problem in redirection

Hi, I am doing this perl script print (@line(1..15)); the lines 1 to 15 get printed... how can i redirect this to file? thanks and regards vivek.s (4 Replies)
Discussion started by: vivekshankar
4 Replies
Login or Register to Ask a Question