Automatically setting your NLS_LANG shell variable


This short script is usefull to automatically set your NLS_LANG shell variable to the correct value, whch mathces to your database. This script connects to the database, retrieves the NLS_LANG value and sets it in your shell. Its called by my rman backup scripts and by my export scripts, both of which, need this variable set. If you are supporting multiple databases which differenct charactersets, this script is a must, unless you want to manually connect to the database, run the queries against v$nls_parameters and set it manually.

#!/bin/ksh

TMPFILE=/tmp/nls_${ORACLE_SID}.tmp

sqlplus -s "/ as sysdba" <<'EOF'>${TMPFILE}
set serveroutput on
set echo off head off feed off
declare
  v_language varchar2(100);
  v_territory varchar2(100);
  v_characterset varchar2(100);
begin
  select value into v_language from v$nls_parameters where parameter='NLS_LANGUAGE';
  select value into v_territory from v$nls_parameters where parameter='NLS_TERRITORY';
  select value into v_characterset from v$nls_parameters where parameter='NLS_CHARACTERSET';
  dbms_output.put_line('export NLS_LANG='||v_language||'_'||v_territory||'.'||v_characterset);
end;
/
EOF
eval $(< ${TMPFILE} )
rm -f ${TMPFILE}
if [ -z ${NLS_LANG} ]
then
echo "JOB_${ORACLE_SID} $prog ERROR. NLS_LANG not set."
else
echo "Setting NLS_LANG to: "$NLS_LANG
fi




Copyright © 2007, All rights reserved by RudnikConsulting Inc