update value in a file using variable passed via unix script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers update value in a file using variable passed via unix script
# 1  
Old 10-28-2010
update value in a file using variable passed via unix script

I have a file in my unix directory called "restart_job1.job". Below is a sample of the script where I am doing a 'grep' to check specifically for an oracle error. If the value 'ORA-" is found, I set a variable to hold the return code value (job1_return_code). If the return code is non zero, I exit the script.
What I need to do update the file "restart_job1.job". There will only be 1 line in this file and it is set to have a value of 0 in the first byte on the first line. In this case, I want to update the file and change the 0 to the job step#.
1) How do I define a variable for the job step? I will setting that value in the failed return code logic.

2) How do I update the "restart_job1.job" file with the value in the job step variable?

Here's my script so far:
Code:
cat $LOG_DIR/job1.out 1>> $JOB_LOG 2>> $JOB_ERR
grep "ORA-" $LOG_DIR/job1.out > /dev/null
job1_return_code=$?
if [ $job1_return_code = 0 ]; then
   /usr/local/bin/mutt -a $JOB_ERR -s "`hostname`: ERROR 101 job1 failed" $MAIL_LIST < $JOB_LOG
   exit 101
fi

It is probably pretty obvious that I am fairly new to unix scripting. Any guidance would be appreciated!!

Moderator's Comments:
Mod Comment Use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 10-29-2010 at 02:15 AM..
# 2  
Old 10-29-2010
Quote:
cat $LOG_DIR/job1.out 1>> $JOB_LOG 2>> $JOB_ERR
I'm a bit worried about this line. What is it intended to do?


Please give examples of "the job step#" in one-digit, two-digit, three-digit form.
Is there anything else in the single record of the file restart_job1.job , and is it a normal unix text file?

Last edited by methyl; 10-29-2010 at 07:04 AM..
# 3  
Old 10-29-2010
You don't give enough details on how your calling the script to check for the Oracle errors, but here's an example of how to accomplish what you're trying to do. You should be able to incorporate this into your existing scripts. If you have questions, let me know.

I don't know what shell you are using. My example uses ksh.

I created two files:

Code:
cat job1.out
line1

cat restart_job1.job 
01234567890123456789

Here's what my script looks like:
Code:
cat chk_job.ksh 
#!/bin/ksh

#
# Define variables.
#
# These values can be passed in
# or you can determine them with 
# logic.  It all depends on how 
# you're calling this script.
#
# For this test we'll just set them
# locally in the script.
#
JOB_STEP=9

#
# Assuming your job logs and restart files
# are using standard naming conventions:
#
#    ie:  job log file:  job1.out
#         restart file:  restart_job1.job
#
JOB_LOG=job1.out
RESTART_FILE=restart_$(echo "${JOB_LOG}" | cut -d\. -f1).job
RESTART_LINE=$(cat ${RESTART_FILE})

#
# Count the number of lines containing
# an Oracle error.
#
ORA_ERROR_COUNT=$(grep "ORA-" job1.out | wc -l)

if [ "${ORA_ERROR_COUNT}" -gt 0 ]; then
   echo "ERROR FOUND"

   #
   # You can change restart_job1.job file
   # at this point.
   #
   echo "${RESTART_LINE}" | sed "s/^0/${JOB_STEP}/" > ${RESTART_FILE}

else
   echo "JOB COMPLETED SUCCESSFULLY"
fi

exit 0

Here's the results based on NO Oracle error being found:

Code:
./chk_job.ksh
JOB COMPLETED SUCCESSFULLY

...and restart_job1.job remains unchanged
Code:
cat restart_job1.job 
01234567890123456789

Now we put an error in job1.out and rerun the chk_job.ksh script:
Code:
cat job1.out
ORA- line1

The error is found and restart_job1.job is updated, replacing the 0 in column one with the job_step (9).
Code:
./chk_job.ksh 
ERROR FOUND

cat restart_job1.job 
91234567890123456789

I hope this helps. Good Luck

Last edited by jsmithstl; 10-29-2010 at 08:18 AM.. Reason: Corrected typos in the comments.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to Update DB table from txt file using CRONJOB in Unix Shell Script

Hi Experts, can guide how we can Update a Database Table using a txt file source Using Unix Shell Scripts. What are the Cron Jobs codes can written to Update DB table. txt file contains record like data. US 09/03/2012 User DocType DocID. these above feilds in txt files need to be updated in... (4 Replies)
Discussion started by: mahesh.sap
4 Replies

2. Shell Programming and Scripting

In the sh file variable is not being passed on.

I am having difficulties with the fllowing script: !/bin/sh voicemaildir=/var/spool/asterisk/voicemail/$1/$2/INBOX/ echo `date` ':' $voicemaildir >> /var/log/voicemail-notify.log for audiofile in `ls $voicemaildir/*.wav`; do transcriptfile=${audiofile/wav/transcript} ... (4 Replies)
Discussion started by: ghurty
4 Replies

3. UNIX for Advanced & Expert Users

Value of variable not getting passed in child script

Hi, I am facing a challenge in fixing an issue in my installation scripts.Here is a situation: There are 3 files which are invoked at a below given order: Installer.ksh----->Installer.xml(Ant script)------->common.ksh I am outputting a message from common.ksh at a terminal, after that trying to... (3 Replies)
Discussion started by: baig_1988
3 Replies

4. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

5. Shell Programming and Scripting

Getting the file name passed to a shell script

Hi all I want to use the filename passed into a shell script within the shell it self. The file will be passed as shown below ./myScript < myFile.file i tried $1 but i think since myFile is not passed as a command line arg its not saving the file name in $1 any help is appreciated. (5 Replies)
Discussion started by: sridanu
5 Replies

6. Shell Programming and Scripting

Shell script in tracking both the passed and failed login in a unix server

Can you help me in providing the following output or a quite similar to this from a shell script ? *** Logins Summary Information ***** ---------------------------------- Failed Login Attempts for Invalid Accounts Date Time IP-ADD Account ... (0 Replies)
Discussion started by: linuxgeek
0 Replies

7. Shell Programming and Scripting

what is the maximum length of a unix shell variable which can be can passed to plsql

what is the maximum length of a unix shell variable which can be can passed to plsql variable:( (1 Reply)
Discussion started by: alokjyotibal
1 Replies

8. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies

9. UNIX for Advanced & Expert Users

unix script for update or insert records from a file to a oracle table

Hi, I have delimited file(|). Sample data: 1|name|50009|DS24|0|12 2|name|30009|DS24|0|13 3|name|20409|DS24|0|14 4|name|20009|DS24|0|15 5|name|10009|DS24|0|16 I want to load this data into a oracle table (update and insert) Please help me the commands and also... (1 Reply)
Discussion started by: unihp1
1 Replies

10. Shell Programming and Scripting

Perl script variable passed to Oracle query

Hi All, I pass a Perl script variable, whch is passed to a query to be prepared. But the problem is I have special character like '&' in this variable which are handled in a special way by the Oracle query parser. How do I get over this? my $cust_name='A&B'; my $sql="Select cust_short_name... (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question