Search code examples
shellshksh

shell script to exclude entries of +ASM, AGENT, -MGMTDB from ORATAB file and just validate only DBSIDs


I have a SQL script which gets details about FRA (Flash recovery Area in ORACLE) which needs to be run only in DB's and not in any other oratab entries. So in the below case, i need it to execute only in dbname1 and dbname2 only and ignore rest of the entries when found.

example oratab file:

dbname1:/u01/app/oracle/11.2.0.4/db_1:Y
dbname2:/u01/app/oracle/12.2.0.1/db_2:Y
+ASM:/u01/app/grid:N
MGMTDB:/u01/app/grid:N
AGENT:/u01/app/agent_home:N

I wrote a function which i am currently using and not working properly as per my requirement. Any changes are greatly appreciated.

checkORATAB()
{
ORATAB=/etc/oratab
SID=`egrep -i ":Y|:N" $ORATAB | cut -d":" -f1 |grep -v "\#"|grep -v "\*"`
export $SID
if [[ `echo "$ORACLE_SID" | cut -b 1` != '+' ]] && [[ "$ORACLE_SID" != "AGENT" ]] && [[ "$ORACLE_SID" != "GRID" ]] && [[ "$ORACLE_SID" != "-MGMTDB" ]] && [[ "$ORACLE_SID" != "+ASM" ]]; then
for i in $SID; do
runscript;
fi
done
exit;
}

Thanks


Solution

  • This is a job for awk or sed although a combination of grep and cut will also work.

    checkORATAB() {
        ORATAB=/etc/oratab
        for sid in $(awk -F: '$1 !~ /(+ASM|MGMTDB|AGENT)/ {print $1}' $ORATAB)
        do
            export ORACLE_SID=$sid
            runscript
        done
    }
    

    Or using sed:

    checkORATAB() {
        ORATAB=/etc/oratab
        for sid in $(sed -n '/^\(+ASM\|MGMTDB\|AGENT\)/b;s/:.*//p' $ORATAB)
        do
            export ORACLE_SID=$sid
            runscript
        done
    }