getting garbage in the output file of shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting garbage in the output file of shell script
# 1  
Old 05-16-2011
getting garbage in the output file of shell script

Hi,
I wrote one shell script and I am calling 1 sql script inside shell script. When I am running the shell script, I am getting actual data as well as garbage data in the output file. Why the garbage is there in the log file. Please help if anybody having any ides.

Script:
-------

Code:
#!/bin/ksh
#set -x
SrcDir=/home/AAAA/BBBB
LogDir=$SrcDir/logs
ErrorDir=$SrcDir/error
SCRIPT_NAME=`basename $0`
Script_Pattern=.sh
PREFIX="TEST_SCRIPT_"
StartTime=`date "+%Y%m%d%H%M%S"`
LogFileName=$PREFIX`basename $SCRIPT_NAME | sed "s/$Script_Pattern$//"`$StartTime.log
PATH=$ORACLE_HOME/bin:$ORACLE_HOME/lib:/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:/usr/contrib/bin/:.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
ORA_NLS33=$ORACLE_HOME/nls/data
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/jdbc/lib
ORACLE_HOME=/ora10g/10.2.0.4
DBCONN=user/password@DBName
# Check for if the script already running
pid=$$
pr_cnt=`ps -ef|grep "$SCRIPT_NAME$" |grep -v grep | grep -v $pid |wc -l`
if [ $pr_cnt -gt 1 ]
then
echo "`date '+%Y-%m-%d %T'`  :: ERROR : Another instance of script for $SCRIPT_NAME is already running." >>  $LogDir/$LogFile
Name
exit 1
else
echo "`date '+%Y-%m-%d %T'`  :: INFO : Upload Process Started" >>  $LogDir/$LogFileName
fi
# ORCALE PATH & VARIABLE For SQL LOADER
export ORACLE_HOME
export ORACLE_SID
export NLS_LANG="AMERICAN_AMERICA.ZHS16GBK"
export NLS_DATE_FORMAT="MM.DD.YYYY HH24:MI:SS"
export LIBPATH=$ORACLE_HOME/lib
export ORACLE_BASE ORACLE_HOME PATH LD_LIBRARY_PATH ORA_NLS33 CLASSPATH
#echo "`date '+%Y-%m-%d %T'`  :: INFO : Create Table Started" 1 >>  $LogDir/$LogFileName
TABLE=`sqlplus -s $DBCONN <<eof
set pagesize 0 feedback off verify off heading off echo off
column num_rows new_value num_rows format 9999
@/home/AAAA/BBBB/TEST_SQL_SCRIPT.sql;
exit
eof`
echo $TABLE >>  $LogDir/$LogFileName
echo "`date '+%Y-%m-%d %T'`  :: INFO : Create Table Ended" >>  $LogDir/$LogFileName
cd $LogDir/
gzip -f $LogFileName
cd -

TEST_SQL_SCRIPT.sql:
--------------------
Code:
DROP TABLE TEST_TEST1_1_3_5_9;
CREATE TABLE TEST_TEST1_1_3_5_9
NOLOGGING
AS
SELECT /*+ PARALLEL(A,8)*/ C.AAA ,
           TO_DATE(SUBSTR(A.BBB, -7, 6), 'yymmdd') ,
           A.BBB DND_BBB,
           A.CCC CCC,
           A.CO_ID,
           UPPER(SUBSTR(BBB, -1)) 
      FROM table1@DB_link A,
           table2@DB_link B,
           table3@DB_link   C
     WHERE A.CO_ID = B.CO_ID
       AND B.DN_ID = C.DN_ID
       AND A.CCC IN ('XXXX','YYYY','ZZZZ','TTTT  ','RRRR  ','RRRR  ','kdlsjfkl  ','asasrre  ','uiowefsj')
       AND B.CCC = '1' AND ( C.GGGG  NOT IN ('O') OR C.GGGG IS NULL)
       AND SUBSTR(A.BBB, -1) IN ('a', 'd')
       AND A.WWW = (SELECT MAX(WWW)
                           FROM table1@DB_link
                          WHERE RRRR = A.RRRR)
        AND TO_CHAR (TO_DATE (SUBSTR (a.BBB, -7, 6), 'yymmdd'),
                   'yyyymmdd'
                  ) =TO_CHAR(SYSDATE-1,'yyyymmdd');


Output(I mean the log file):
--------------------------

Code:
2011-05-16 21:08:34  :: INFO : Upload Process Started
DROP TABLE TEST_TEST1_1_3_5_9 Archive DIR1 DIR10 DIR2 DIR3 DIR4 DIR5 DIR6 DIR7 DIR8 DIR9  
a123.txt abc_def_ytr.txt error ftp_201104141824.log.gz  lib logs mainscript.sh new_file new_file.pub newdir 
newfile nohup.out script123.sh script123_orig.sh script_for_sftp.sh script_sftp_1.sh sftpscript_imp.sh subscript.sh 
test1.sh test123 test123.sh test_shell_script.sh ERROR at line 1: 
ORA-00942: table or view does not exist ) =TO_CHAR(SYSDATE-1,'yyyymmdd') 
Archive DIR1 DIR10 DIR2 DIR3 DIR4 DIR5 DIR6 DIR7 DIR8 DIR9 
test_shell_script.sh ERROR at line 23: ORA-12801: error signaled in parallel query 
server P012 ORA-01847: day of month must be between 1 and last day of month ORA-02063: preceding 2 lines from 
table1@DB_link
2011-05-16 21:08:35  :: INFO : Create Table Ended


why the output contains the data other than the script. How to remove this unnecessary data.

Moderator's Comments:
Mod Comment Please start using [CODE] tags

Last edited by pludi; 05-16-2011 at 01:35 PM..
# 2  
Old 05-16-2011
I guess that you need to first separate stdout from stderr when you call the script
./script.sh > stdout.log 2>stderr.log

Also, I'd redirect to /dev/null the ouput from calls like:
pr_cnt=`ps -ef|grep "$SCRIPT_NAME$" |grep -v grep | grep -v $pid |wc -l`
But it could be better to simple redirect what you are looking to an individual file.

Last command also could be rewritted to something nicer...

ps -p $pid >/dev/null && {
echo "`date '+%Y-%m-%d %T'` :: ERROR : Another instance of script for $SCRIPT_NAME is already running." >> $LogDir/$LogFileName
exit 1
} || {
echo "`date '+%Y-%m-%d %T'` :: INFO : Upload Process Started" >> $LogDir/$LogFileName
}

Last edited by manuu; 05-16-2011 at 01:38 PM.. Reason: mistaken logic
# 3  
Old 05-17-2011
Quote:
echo $TABLE >> $LogDir/$LogFileName
This is the main problem line.
The real problem is that you are trying to store a multi-line output from an entire Oracle sqlplus session into an environment variable. The echo ... line is expanding to a very wide line including shell expansion of "**" (from the Oracle error message) which gives a directory list! The line-breaks are lost when you store this sort of output into an Environment Variable and you just get a long list of words.

Purely from a syntax point of view should perhaps be:
Code:
echo "$TABLE" >>  $LogDir/$LogFileName

However Environment Variables are not files. You should not be storing multi-line output into an Environment Variable.
Use an Oracle "spool" command to write your Oracle output to your output file and used shell redirect to write any left-over messages to an error log file.

Ps. The only time I ever see posts showing sqlplus output to an Environment Variable is from India. I suspect that there are some duff course notes in circulation or maybe there is a duff reference book?
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

3. Programming

How to redirect the output of a shell script to a file?

hi, i have a html form which call a perl program, this perl program calls a shell script. <html> <head> <title>demo</title> </head> <body> <form name="frm1" action="/cgi-bin/perl_script.pl" method="post"> <input type="text" name="fname"> ... (1 Reply)
Discussion started by: Little
1 Replies

4. Shell Programming and Scripting

Ignore garbage output file

Hi All, below is my shell script #!/bin/sh set -x echo "test for multiple values" UIDPSWD=`cat /projects/feeds/twest/uidpswd` echo "oracle connection test" full=/projects/feeds/twest/test_file values=`cut -d'|' -f1 $full|sed -e "s/.*/'&'/" -e 's/$/,/g' -e '$s/,$//'` sqlplus $UIDPSWD... (2 Replies)
Discussion started by: krupasindhu18
2 Replies

5. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

6. Shell Programming and Scripting

getting garbage in the output file of shell script

Hi Everyone, The problem is that I am getting messages other than the script in the current log file. Ideally the script should contain only the messages that are redirected to the log file. How to remove these unwanted data from the log file. Please help if you have any idea how to remove the... (0 Replies)
Discussion started by: vsachan
0 Replies

7. Shell Programming and Scripting

Execution Output of a shell script into a file.

Hi Experts, I have a script called test.sh. I am trying to execute it with sh -x test.sh. Where i can find sequence of steps executed one by one. Now i want to these executions to be captured in a file. i.e sh -x test.sh > output.txt the above one is notworking. can anyone help me... (6 Replies)
Discussion started by: naree
6 Replies

8. Shell Programming and Scripting

Remove Garbage Output

Hello Friends, In a script i m using different temporary file and i remove them in the end. During script execution i have some garbage output which is not required. For example: Garbage Output ++ rm temp_out temp_a temp_b temp_c ++ rm Filter1 Filter2 Script : Even i am redirecting rm... (7 Replies)
Discussion started by: Danish Shakil
7 Replies

9. Shell Programming and Scripting

vi command -output garbage char in HP-UX

Hi all , I am new to HP-UX flavour of unix. i am issuing simple "vi" comand on the command prompt it is showing me some garbage character in command prompt itself ..unreadable format. I tried opening an existing file using the vi editor --and same thing ... (3 Replies)
Discussion started by: jambesh
3 Replies

10. Shell Programming and Scripting

Removing Garbage output

I am using following code to read myfile.ddl line by line. But the thing is it is printing lot of garbage which are the names of the files and directories in which myfile.ddl is present. Kindly refine the code so that only myfile.ddl contents are only read LOGFILE="logfile.txt"... (4 Replies)
Discussion started by: skyineyes
4 Replies
Login or Register to Ask a Question