# mydb01:/orainst/product/19c:Y cdb:/orainst/product/19c:NTo extract the database names:
grep "^[^#]" /etc/oratab | awk -F':' '{print $1}'Result:
mydb01 cdb
Minimalistic Oracle contains a collection of practical examples from my encounters with Oracle technologies. When relevant, I also write about other technologies, like Linux or PostgreSQL. Many of the posts starts with "how to" since they derive directly from my own personal experience. My goal is to provide simple examples, so that they can be easily adapted to other situations.
# mydb01:/orainst/product/19c:Y cdb:/orainst/product/19c:NTo extract the database names:
grep "^[^#]" /etc/oratab | awk -F':' '{print $1}'Result:
mydb01 cdb
for l in $(cat file.txt|grep 'Status'| awk -F' ' '{print $2}'); do
if [ $l != "SUCCESS" ]; then
echo $l ": Errors found. Please check logfile status.log"
else
echo "Readjust memory for the remaining cdb instance"
su - oracle <<!
-- code here --
!
runAdjustMemory
fi
done
During execution, my script failed with
./myscript.sh: line 32: warning: here-document at line 20 delimited by end-of-file (wanted `!') ./myscript.sh: line 33: syntax error: unexpected end of file
for l in $(cat file.txt|grep 'Status'| awk -F' ' '{print $2}'); do
if [ $l != "SUCCESS" ]; then
echo $l ": Errors found. Please check logfile status.log"
else
echo "Readjust memory for the remaining cdb instance"
su - oracle <<!
-- code here --
!
runAdjustMemory
fi
done
while true; do if [ -f /u01/createdb.log ]; then tail -f /u01/createdb.log fi done
pmon=`ps -fu oracle | grep pmon_$1 | grep -v grep | awk '{ print $8}'`
echo $pmon
ora_pmon_cdb ora_pmon_mydb01
for pname in $pmon; do
echo $pname
done
Output:
ora_pmon_cdb ora_pmon_mydb01The for loop construct seem to take space as a delimter by default, so in this case it works out well.
analyze_status=`cat /u01/oracle/cfgtoollogs/upgrade/auto/autoupgrade.log | grep 'ANALYZE and FIXUPS' | awk 'NR==1'`Output of the command is:
echo $analyze_status ANALYZE and FIXUPS modes are not run when converting a Non-CDB database to a PDBWithout the NR==1 I get the same sentence repeated twice:
ANALYZE and FIXUPS modes are not run when converting a Non-CDB database to a PDB ANALYZE and FIXUPS modes are not run when converting a Non-CDB database to a PDB
su - oracle <<! echo " set lines 10 set pages 0 set trimspool on set verify off set heading off set feedback off set echo off spool /tmp/count_options.out SELECT TRIM(COUNT(*)) FROM DBA_REGISTRY; " > /tmp/count_options.sql sqlplus -s / as sysdba @/tmp/count_options.sql !Output is:
/tmp]# cat count_options.out 4Note that without the TRIM function, the output will be
/tmp]# cat count_options.out 4The -s switch will execute sqlplus silently, without it, the output would be
cat count_options.out 4 SYS@mydb01>SQL>
echo "Test: are there subdirectories in /orainst/oracle/cfgtoollogs/dbca/cdb ?" ls $(pwd)/*/ > /dev/null 2>&1 if [ $? == 0 ]; then echo "Yes" export PDBDIR=`ls -d */ | awk -F '/' '{ print $1 }'` grep ERROR --exclude-dir=${PDBDIR} * | egrep -v $ignorelist else echo "No" grep ERROR * | egrep -v $ignorelist fiThe code above is based on a finding in this post on Stack Overflow.
cd /orasw/oracle/cfgtoollogs/dbca/cdb export PDBDIR=`ls -d */ | awk -F '/' '{ print $1 }'` export ignorelist='ORA_IGNORE_CVU_ERRORS|exectask.sh|utlrp0.log|RMANUtil\$RMANUtilErrorListener.handleError:1386|RMANUtil\$RMANUtilErrorListener.handleError:1383|RMANEngine.readSqlOutput:1046' grep ERROR --exclude-dir=${PDBDIR} * | egrep -v $ignorelistThe errors I have excluded are based on my own observations on what's important or not during dbca usage.
# 080224: Created # Check if there is only one controlfile present. If yes, create two new ones in the desired locations #!/bin/bash #cleanup old files chkcntrfiles() { rm -f /tmp/chk_cntrfile_location.sql rm -f /tmp/chk_cntrfile_location.log sqlfile=/tmp/chk_cntrfile_location.sql logfile=/tmp/chk_cntrfile_location.log echo "Check controlfile locations in database " $ORACLE_SID cat << EoF > ${sqlfile} set lines 200 heading off feedback off verify off echo off termout off set pages 0 set trimspool on col name format a70 spool ${logfile} select name from v\$controlfile; exit EoF cd /home/oracle/&&sqlplus -s -L / as sysdba @${sqlfile} } chkcntrfiles if [ `cat ${logfile} | wc -l` -ge 2 ]; then echo "There are at least 2 controlfiles present" else echo "There is only one controfile present." echo "Will now shutdown db and make another copy" echo "shutdown immediate" | sqlplus / as sysdba export org_contr_file=`cat ${logfile}` cp -v $org_contr_file /disk1/oradata/$ORACLE_SID/control01.ctl cp -v $org_contr_file /disk2/fra/$ORACLE_SID/control02.ctl echo "startup nomount" | sqlplus / as sysdba echo "alter system set control_files='/disk1/oradata/$ORACLE_SID/control01.ctl','/disk2/fra/$ORACLE_SID/control02.ctl' scope=spfile;" | sqlplus / as sysdba echo "startup mount force;" | sqlplus / as sysdba echo "Check new controlfile locations in database " $ORACLE_SID chkcntrfiles if [ ! `cat ${logfile} | wc -l` -ge 2 ]; then echo "We tried creating two new controlfiles but failed for some reason. Manual checkup required." exit 1 fi echo "Alles OK!" echo "alter database open;" | sqlplus / as sysdba fi exit
read -p 'Will you be administrating the cdb or the PDB? ' container case $container in CDB|cdb) . $ORACLE_HOME/cdb_settings.env; alias sql='sqlplus / as sysdba';; PDB1|pdb1) . $ORACLE_HOME/pdb1.env; alias sql='sqlplus sys@pdb1 as sysdba';; *) echo "You must select cdb or pdb1"; exit 1;; esac
initParams=with
initParams=undo_tablespace=UNDOTBS1,sga_target=9GB,db_block_size=32768BYTES,control_files=\"\/u02\/oradata\/{DB_UNIQUE_NAME}\/control01.ctl\"\,\"\/recovery\/fra\/{DB_UNIQUE_NAME}\/control01.ctl\"),db_recovery_file_dest=\/recovery\/fra\/{DB_UNIQUE_NAME}in the file "myresponsefile.rsp":
sed -i "s/initParams=/initParams=undo_tablespace=UNDOTBS1,sga_target=9GB,db_block_size=32768BYTES,control_files=\"\/u02\/oradata\/{DB_UNIQUE_NAME}\/control01.ctl\"\,\"\/recovery\/fra\/{DB_UNIQUE_NAME}\/control01.ctl\"),db_recovery_file_dest=\/u04\/recovery\/{DB_UNIQUE_NAME} /" myreponse_file.rspNote that all double quotes (") and forward (/) slashes must be masked by the backslash charcater \ to be interpreted correctly.
PDB1_test1.oric.noIn other words, the files listener.ora, sqlnet.ora and tnsnames.ora are symlinks pointing to this subdirectory:
# ls -altr lrwxrwxrwx 1 oracle dba 34 Feb 10 13:29 sqlnet.ora -> PDB1_test1.oric.no/sqlnet.ora lrwxrwxrwx 1 oracle dba 36 Feb 10 13:29 tnsnames.ora -> PDB1_test1.oric.no/tnsnames.ora lrwxrwxrwx 1 oracle dba 36 Feb 18 12:17 listener.ora -> PDB1_test1.oric.no/listener.ora drwxr-xr-x 2 oracle dba 4096 Feb 18 12:18 PDB1_test1.oric.no drwxr-xr-x 5 oracle dba 4096 Feb 18 12:19 .If you are to exchange a string in these files, for example when switching to a new version of the Oracle software, make sure you use the directive --follow-symlinks to preserve your symlinks:
export PRE_ORACLE_VERSION=19.17 export NEW_ORACLE_VERSION=19.18 cd $TNS_ADMIN sed --follow-symlinks -i "s/$PRE_ORACLE_VERSION/$NEW_ORACLE_VERSION/g" listener.oraIf you don't, the listener.ora will be placed directly in $TNS_ADMIN, and the symlinks will be dropped.
su - oracle <<! instructions here !I was not able to save the value of a string extracted from an xml file in a session variable.
su - oracle <<! export CNTXT_FILE_PERL_VERSION=\`grep PERL5LIB \$CONTEXT_FILE | awk -F "site_perl/" '{print \$2}' | cut -f 1 -d":"\` !
su - oracle -c "/tmp/set_params.sh"You may see errors like the following:
set_params.sh: line 6: $'\r': command not found set_params.sh: line 42: syntax error: unexpected end of fileRoot cause:
dos2unix set_params.sh dos2unix: converting file flexpod_params.sh to Unix format...When executed again, the above errors should be gone!
#!/bin/bash
export SCRIPT_NAME=`basename $0`
export TS=`date +\%m.\%d.\%y\_%H_%M_%S`
export GLOGFILE=/u01/mylogfile_${TS}.log
touch ${GLOGFILE}
chmod 666 ${GLOGFILE}
exec 1> ${GLOGFILE} 2>&1
echo "Starting job at " `date`
your shell instructions here
echo "Ending job at " `date`
exit
export HOST=${HOSTNAME%%.*}
# check for installation files existence if [ ! -f /tmp/file1 -a -f /tmp/file2 -a -f /tmp/file3 ] then echo "Some of the files file1, file2 or file3 are missing from /tmp " exit 1 fi
Clone Volume: true Clone Parent server Name: myserver1-cluster49 SnapClone Parent Volume: myvolume_mirror1
cat clonetext.txt | grep Clone Clone Volume: true Clone Parent server Name: myserver1-cluster49 SnapClone Parent Volume: myvolume_mirror1Grepping for the 4th column helps, but you still get two rows, not just the single one you're interested in:
oracle@oric-db01:[mydb01]# cat clonetext.txt | grep Clone | awk -F' ' '{print $4}' Name: myvolume_mirror1Adding the NR flag to your command solves the problem:
cat clonetext.txt | grep Clone | awk -F' ' 'NR==3 {print $4}'
myvolume_mirror1
printf "Parameters passed are : $*\n"
#!/bin/bash . /home/oracle/.bash_profile userid=$1 password=$2 userexist="WHENEVER SQLERROR EXIT;" echo -e "alter session set container=PDB01;\n$userexist\nalter user $userid identified by $password account unlock;" | sqlplus -L -s / as sysdba