Unix Programming - Using Environemt Variables

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2007 > Using Environemt Variables





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 Using Environemt Variables
amerar@iwc.net

2007-02-21, 1:18 pm

Hi All,

We are trying to develop a standard environment include for each of
our scripts. Basically at the top of each script, we will include
that environemt file.

However, there is one issue, for some reason, $0 is not being resolved
into the script name??? Please review this clip of our include file:

operation="Test Script"
.. /rm5/prod/control/header.env


HEADER.ENV
-------------------------
dte=`date +%m%d%Y`
tme=`date +%m%d%Y%H%M%S`
scr_name=`echo $0 | awk -F"/" '{print $5}'`
log_name=`echo $0 | awk -F"/" '{print $5}' | awk -F"." '{print $1}'`
logfile=$LOGDIR/$log_name.$dte
today=`/bin/date +%c`

echo "================= START OF SCRIPT ==============" >> $logfile
echo "Start Time : $today" >> $logfile
echo "Script Name : $scr_name" >> $logfile
echo "Operation : $operation" >> $logfile
echo "Process ID : $$" >> $logfile


Why is $0 not getting resolved???

Bit Twister

2007-02-21, 1:18 pm

On 21 Feb 2007 07:23:22 -0800, amerar@iwc.net wrote:
> Hi All,
>
> We are trying to develop a standard environment include for each of
> our scripts. Basically at the top of each script, we will include
> that environemt file.
>
> However, there is one issue, for some reason, $0 is not being resolved
> into the script name??? Please review this clip of our include file:


I would add
_exe=$0
above dte=`date +%m%d%Y`
and change $0 to $_exe throughout your script.

Still broke.
Add
set -x
(some code to check)
(some code to check)
set -
to see what is going on.
Chris F.A. Johnson

2007-02-21, 1:18 pm

On 2007-02-21, amerar@iwc.net wrote:
> Hi All,
>
> We are trying to develop a standard environment include for each of
> our scripts. Basically at the top of each script, we will include
> that environemt file.
>
> However, there is one issue, for some reason, $0 is not being resolved
> into the script name??? Please review this clip of our include file:
>
> operation="Test Script"
> . /rm5/prod/control/header.env
>
>
> HEADER.ENV
> -------------------------
> dte=`date +%m%d%Y`
> tme=`date +%m%d%Y%H%M%S`


That will be inaccurate if the date changes between calls to date.
Use a single invocation of date:

eval $( date "+ dte=%m%d%Y tme=%m%d%Y%H%M%S" )

If you plan to use the date or time later in the script (and if
not, why bother with it?), it would be better to store each
component in a separate variable, e.g.:

eval "$( date "+
DATE=%Y-%m-%d
YEAR=%Y
MONTH=%m
DAY=%d
TIME=%H:%M:%S
HOUR=%H
MINUTE=%M
SECOND=%S
datestamp=%Y-%m-%d_%H.%M.%S
DayOfWeek=%a
DayOfMonth=%d
DayOfYear=%j
DayNum=%w
MonthAbbrev=%b
MonthFull=%B
MonthName=%B
DayName=%A
"
)"


> scr_name=`echo $0 | awk -F"/" '{print $5}'`


No need for awk:

scr_name=${0##*/}

> log_name=`echo $0 | awk -F"/" '{print $5}' | awk -F"." '{print $1}'`


You already have the first part of the pipeline in $scr_name; why
repeat the call to awk?

log_name=`echo "$scr_name" | awk -F"." '{print $1}'`

But you don't need awk for that:

log_name=${screen%%.*}


You seem to be making some assumptions about the path to the
script and the format of the script's name (and the contents of
$0). Are you sure you can count on these?

> logfile=$LOGDIR/$log_name.$dte
> today=`/bin/date +%c`


See comment above on use of date.

> echo "================= START OF SCRIPT ==============" >> $logfile
> echo "Start Time : $today" >> $logfile
> echo "Script Name : $scr_name" >> $logfile
> echo "Operation : $operation" >> $logfile
> echo "Process ID : $$" >> $logfile


{
echo "================= START OF SCRIPT =============="
echo "Start Time : $today"
echo "Script Name : $scr_name"
echo "Operation : $operation"
echo "Process ID : $$"
} >> $logfile

> Why is $0 not getting resolved???


What does "not getting resolved" mean?

What does happen?

Did you check the value of $0 before piping it (unnecessarily)
through awk?


--
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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com