|
Home > Archive > Unix Shell > January 2006 > How to create a module for logic that is repeated in the script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to create a module for logic that is repeated in the script
|
|
|
| Helllo!
I have a script which has application specific commands which run
sequentially. For each command I have logic below that checks if the
process ran successfully, if there were errors in the log and if the
report was created.
So how do i create a module/fuction - so that I don't have to copy the
whole module again. And it become usable by others...
Below is the snippet that I have to repeat.
Any help will be appreciated.
Thanks!
Indu
========================================
=============================
if [ $? -ne 0 ]; then
echo "Report $ID failed: Return code - $?" >> $STATUS
else
log_file=`ls -t1 $RPT_DIR/ATL_extract.tcl*log|head -1`
echo "log file - $log_file" >> $STATUS_FILE
$UTIL_DIR/cf_extract.sh $EXP_DIR/ATL.extract.other413_base.tab
$RPT_DIR/$OUTPUT >> $STATUS
#error_line_cnt=`cat $log_file|egrep -i "ERROR|WARNING"|wc -l`
error_line_cnt=`cat $log_file|egrep -i "ERROR"|wc -l`
echo "Error count : $error_line_cnt" >> $STATUS
if [ $error_line_cnt -gt 0 -o ! -s $RPT_DIR/$OUTPUT ]; then
echo "Report $ID failed:Return code-$? and/or Output file
$RPT_DIR/$OUTPUT not found and/or $error_line_cnt no. of errors found"[vbcol=seagreen]
else
echo "Report $ID completed successfully" >> $STATUS
fi
fi
| |
| Chris F.A. Johnson 2006-01-29, 9:30 pm |
| On 2006-01-25, Indu wrote:
> Helllo!
>
> I have a script which has application specific commands which run
> sequentially. For each command I have logic below that checks if the
> process ran successfully, if there were errors in the log and if the
> report was created.
>
> So how do i create a module/fuction - so that I don't have to copy the
> whole module again. And it become usable by others...
>
> Below is the snippet that I have to repeat.
Put the code in a file and call that file in any script that needs
it.
If you need access to the variables in the code, put it in a
function:
name_of_function()
{
: code goes here
}
and save that to a file. When you need it in a script,
source the file and call the function as necessary.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
|
|
Chris F.A. Johnson wrote:
> On 2006-01-25, Indu wrote:
>
> Put the code in a file and call that file in any script that needs
> it.
>
> If you need access to the variables in the code, put it in a
> function:
>
> name_of_function()
> {
> : code goes here
> }
>
> and save that to a file. When you need it in a script,
> source the file and call the function as necessary.
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence
Hello Chris,
Attached are the three files I created. I'd appreciate your feedback:
I created three files:
Variables: var.sh
===================
#!/usr/local/bin/ksh
ACCRUAL_DATE=07-31-05
ACCRUAL=1
CLIENT_DIR=xxx
DATABASE=atl_42
DEBUG="echo"
DEBUG=""
EXT=4.2FATL
MODELGEN_FILE=/xxx/xxx/reports/xx/xxx.txt
PREV_DATE=06-29-05
RUN_DATE=06-30-05
RPT_DIR=/xx/xxx/xxx/xxx/Y${RUN_DATE##*-}/$xxx
RUN_DATE_1=07-01-05
MKTENV=xxxx
STATUS_FILE=/xxx/xxxxxa/xxxx/xxx/xx.txt
UTIL_DIR=/homes/xx/xx/xx/tsx
USER=xxxx
VERSION=/bin/Driver
EXP_DIR=/xx/xx/reports
########################################
#######################
Function : func.sh (snippet that is repeated)
#!/usr/local/bin/ksh
.. /homes/qa/shah/temp/vari.sh
if [ $? -ne 0 ]; then
echo "Report $ID failed: Return code - $?" >> tmp.txt
else
log_file=`ls -t1 $RPT_DIR/aaa_extract.tcl*log|head -1`
echo "log file - $log_file" >> $STATUS_FILE
$UTIL_DIR/cf_extract.sh $EXP_DIR/aaa.extract.other413_base.tab
$RPT_DIR/$OUTPUT >> tmp.txt
error_line_cnt=`cat $log_file|egrep -i "ERROR"|wc -l`
echo "Error count : $error_line_cnt" >> $STATUS_FILE
if [ $error_line_cnt -gt 0 -o ! -s $RPT_DIR/$OUTPUT ]; then
echo "Report $ID failed:Return code-$? and/or Output file
$RPT_DIR/$OUTPUT not found and/or $error_line_cnt no. of errors found"[vbcol=seagreen]
else
echo "Report $ID completed successfully" >> tmp.txt
fi
fi
###################################
And finally the file I want to run: rthis.sh
#!/usr/local/bin/ksh
.. /homes/temp/vari.sh
.. /homes/func.sh
$DEBUG /usr/bin/time $VERSION -m $CLIENT_DIR -D $RUN_DATE -e
-c"TCL:aaa: -A aaa-S 01/01/1992 -E 09/04/2007 -e other${EXT} -u
:extract.tcl") 2>> tmp.txt
But as soon as I run the command I get the error : /rthis.sh: No such
file or directory
Thanks for your help...
Indu
| |
| Chris F.A. Johnson 2006-01-29, 9:31 pm |
| On 2006-01-25, Indu wrote:
>
> Chris F.A. Johnson wrote:
>
> Attached are the three files I created. I'd appreciate your feedback:
>
> I created three files:
>
> Variables: var.sh
> ===================
[snip]
> ########################################
#######################
>
> Function : func.sh (snippet that is repeated)
>
> #!/usr/local/bin/ksh
> . /homes/qa/shah/temp/vari.sh
>
[snip]
> ###################################
>
> And finally the file I want to run: rthis.sh
>
> #!/usr/local/bin/ksh
>
> . /homes/temp/vari.sh
[snip]
>
> But as soon as I run the command I get the error : /rthis.sh: No such
> file or directory
I see "var.sh", "/homes/qa/shah/temp/vari.sh" and
"/homes/temp/vari.sh"; which is it?
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| Bill Marcum 2006-01-29, 9:31 pm |
| On 25 Jan 2006 15:00:21 -0800, Indu
<ashah75@gmail.com> wrote:
>
> And finally the file I want to run: rthis.sh
>
> #!/usr/local/bin/ksh
>
> . /homes/temp/vari.sh
> . /homes/func.sh
>
> $DEBUG /usr/bin/time $VERSION -m $CLIENT_DIR -D $RUN_DATE -e
> -c"TCL:aaa: -A aaa-S 01/01/1992 -E 09/04/2007 -e other${EXT} -u
>:extract.tcl") 2>> tmp.txt
>
>
> But as soon as I run the command I get the error : /rthis.sh: No such
> file or directory
>
Are you giving the command "/rthis.sh" instead of "./rthis.sh"?
--
Planet Claire has pink hair.
All the trees are red.
No one ever dies there.
No one has a head....
| |
|
| i am giving ./rthis.sh
and the file is indeed vari.sh ( in case above it is a typo)
I also verified the path names and they seem correct.
Thanks!
Alok Shah
|
|
|
|
|