Operating System - Linux
1753797 Members
8293 Online
108805 Solutions
New Discussion юеВ

Re: Qualifying variables across files

 
SOLVED
Go to solution
RobinKing
Valued Contributor

Qualifying variables across files

Looking for a bit of help with a 'simple' script to role some WebSphere logs. I have three servers with multiple logs on each. The path to the logs on each server is different by only one directory name eg:

Server1:/opt/IBM/WebSphere/AppServer/profiles/Dmgr01/logs/dmgr/SystemOut.log

Server2:/opt/IBM/WebSphere/AppServer/profiles/Dmgr02/logs/dmgr/SystemOut.log

Server3:/opt/IBM/WebSphere/AppServer/profiles/Dmgr03/logs/dmgr/SystemOut.log

I have more than one file on each server, but for the purposes of this I'll keep it simple.

I have a reference file with a list of logs, and I've sumplimented the Dmgr0n with a variable $DMGR. I then have a script that should substitute $DMGR with the actual name, then role the logs.

The script:
~~~~~~~~~~~~~~~~~~~~~~~~
DATE=`date +%b%o`
WASLOGS=/sysadmin/unix/scripts/puma/WASlogs
HOST=$1

case $1 in
Server1)
DMGR=Dmgr01
;;
Server2)
DMGR=Dmgr02
;;
Server3)
DMGR=Dmgr03
;;
esac

echo $DMGR

for FILE in `cat $WASLOGS`
do
echo $FILE
if [ -f $FILE ]
then
OLDFILE=$FILE.$DATE
mv $FILE $OLDFILE 2>&1
> $FILE
fi
done

~~~~~~~~~~~~~~~~~~~~~~~~
The referenced file contains: /opt/IBM/WebSphere/AppServer/profiles/$DMGR/logs/dmgr/SystemOut.log

The script, when run doesn't recognise the $DMGR in the reference file as a variable, it translates it as a string.

root@Server1:/sysadmin/unix/scripts > ./rolewaslogs.sh Server1
+ + date +%b%o
DATE=Oct07
+ WASLOGS=/sysadmin/unix/scripts/WASlogs
+ HOST=Server1
+ DMGR=Dmgr01
+ echo Dmgr01
Dmgr01
+ cat /sysadmin/unix/scripts/WASlogs
+ [ -f /opt/IBM/WebSphere/AppServer/profiles/$DMGR/logs/dmgr/SystemOut.log ]

Can anyone explain how i can get my script to qualify the variables I'm trying to set in the reference file?

Thanks
6 REPLIES 6
Oviwan
Honored Contributor

Re: Qualifying variables across files

Hey

you could also grep the $WASLOGS file like this:

for FILE in $(grep ${DMGR} $WASLOGS)

then you get only the files which contains $DMGR

hope this helps

Regards
RobinKing
Valued Contributor

Re: Qualifying variables across files

Everyline in the reference file will be a vlid log file, so I won't need to filter them
Oviwan
Honored Contributor

Re: Qualifying variables across files

oh sorry misunderstood...
you can source the WASLOGS file and redirect it like this:

....
echo $DMGR
#this will "translate" the ${DMGR} variable in the file

. $WASLOGS >> newlogfile #dot space $WASLOGS
for FILE in `cat newlogfile`
....

and another thing write the DMGR variable in the WASLOGS file like this ${DMGR} instead of $DMGR

Regards

James R. Ferguson
Acclaimed Contributor
Solution

Re: Qualifying variables across files

Hi:

You need to use 'eval' to cause the parameter substitution:

#!/usr/bin/sh
DMGR=server01
for FILE in `cat WASLOGS`
do
WHICH=$(eval echo ${FILE})
echo ${WHICH}
if [ -f "${WHICH}" ]; then
echo "ok"
else
echo "no file"
fi
done

Regards!

...JRF...
RobinKing
Valued Contributor

Re: Qualifying variables across files

Oviman

I've tried that:


echo $DMGR

. ${WASLOGS} >> newlog
echo ${DMGR}
for FILE in `cat newlog`
do
if [ -f ${FILE} ]
then
OLDFILE=${FILE}.${DATE}
mv ${FILE} ${OLDFILE} 2>&1
> ${FILE}
fi
done

root@hostname:/sysadmin/unix/scripts > cat WASlogs
/opt/IBM/WebSphere/AppServer/profiles/${DMGR}/logs/dmgr/SystemOut.log

It seems to qualify the variable, but gives the following error:

+ . /sysadmin/unix/scripts/WASlogs
+ 1>> newlog
+ /opt/IBM/WebSphere/AppServer/profiles/Dmgr01/logs/dmgr/SystemOut.log
./rolewaslogs.sh[35]: /opt/IBM/WebSphere/AppServer/profiles/Dmgr01/logs/dmgr/SystemOut.log: Execute permission denied.
+ echo Dmgr01
Dmgr01
+ cat newlog
RobinKing
Valued Contributor

Re: Qualifying variables across files

James, that worked perfectly! Thanks!