I have always been coding more or less in my life producing some useful scripts mainly for the Oracle community. During the last 1+ year, I have coded more for a big project to move data from Teradata to Google Cloud and also participated in this Google open source project.
Coding more and for more intensively used scripts made me faced new problems and then found new solutions / tips which I now apply in every of my scripts. Indeed, it is not because a script is not heavily used every 5 minutes to move TBs of data from a system A to a system B that it does no deserve to be nice and robust.
These below tips do not pretend to be exhaustive nor to be a kind of ulimate shell guide nor to be super obfuscated rocket science, it is just few tips I found very useful to know and to use in every script you could write to make them nice, easily maintainable and robust:
Coding more and for more intensively used scripts made me faced new problems and then found new solutions / tips which I now apply in every of my scripts. Indeed, it is not because a script is not heavily used every 5 minutes to move TBs of data from a system A to a system B that it does no deserve to be nice and robust.
These below tips do not pretend to be exhaustive nor to be a kind of ulimate shell guide nor to be super obfuscated rocket science, it is just few tips I found very useful to know and to use in every script you could write to make them nice, easily maintainable and robust:
- 1/ Use mktemp for your tempfiles
- 2/ Use trap to make your scripts more robust
- 3/ Exit codes management with pipelines
- 4/ Prevent 2 concurrent executions of a script
- 5/ Protect and quote your variables
- 6/ && and ||
- 7/ Single quotes vs double quotes
- 8/ Check many variabes at once with indirect expansion
- 9/ Pause and restart a process
- 10/ Run a command for a certain amount of time
- 11/ Unleash your shell power with ... "| bash" !
- 12/ Variables manipulation
- 13/ tac
- 14/ rev
- 15/ comm
- 16/ awk Tutorial
- 17/ SECONDS
- 17/ paste (combined with fold, shuf and column)
Thank you for yours scripts and posts.!
ReplyDeleteHi Sir,
ReplyDeleteI'm a regular follower of your blog. This series of bash knowledge is really fantastic for me. I'm eagerly waiting for rest of the articles.
Thannk you
Thanks a lot ! I add some as they come :)
DeleteIf you have any specific topic you would like to be documented here, let me know.
Hi Sir,
ReplyDeleteHope you are good and healthy.
I have developed one shell script to loop through all my pdb's (Pluggable databases) to get the health details like Tablespaces, Invalid objects, long running etc.
The script is failing to execute. Please let me know what i need to do to loop through all my PDBS. My CDB contains 4 pdbs
Script
=======
export ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export TNS_ADMIN=/home/oracle/monitoring_scripts
### connecting CDB here
${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" < 2 AND open_mode = 'READ WRITE' ORDER BY 1
EOF)
do
(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
alter session set container=${PDB};
@/home/oracle/monitoring_scripts/pdb_health.sql
spool off
EOF
)
done
Error
======
./monitor_space_2.sh: line 11: syntax error near unexpected token `('
./monitor_space_2.sh: line 11: `for PDB in (${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF'
Like this:
Deletefor PDB in $(sqlplus -S / as sysdba << END
set head off ;
set feed off ;
select name from v\$pdbs where open_mode = 'READ WRITE' ;
END
); do
echo $PDB;
sqlplus -S / as sysdba << END
alter session set container="${PDB}" ;
show con_name ;
END
done
PS: the heredoc must not have anything behind:
Deletefor PDB in $(sqlplus -S / as sysdba << END
set head off ;
set feed off ;
select name from v\$pdbs where open_mode = 'READ WRITE' ;
END); do <=== wrong
for PDB in $(sqlplus -S / as sysdba << END
set head off ;
set feed off ;
select name from v\$pdbs where open_mode = 'READ WRITE' ;
END <== good
); do
Thank you very much for your update sir. I'll try your code and let you know the status.
Delete