Operating System - HP-UX
1834366 Members
2006 Online
110066 Solutions
New Discussion

Re: How to assign a variable containing other variables

 
SOLVED
Go to solution
Manuales
Super Advisor

How to assign a variable containing other variables

Hi ..
how can assign the value to the variable named "LOG"

LOG=$($log/$(echo $0 | cut -d"/" -f5 | cut -d"." -f1))

if i run above, there is an error:
$ 01_bdf_filesystem.sh
01_bdf_filesystem.sh[28]: /home/MONITOR/LOG/01_bdf_filesystem: cannot execute
esto vale log:


please let me knnow.
5 REPLIES 5
Manuales
Super Advisor

Re: How to assign a variable containing other variables

This is the script:
Name of the script: 01_bdf_filesystem.sh

. $HOME/ENVIRONMENT/environment.sh
typeset -i b=0 u=$1

LOG=$($log/$(echo $0 | cut -d"/" -f5 | cut -d"." -f1))

echo esto vale log: $LOG
read jkjkjk

for a in `bdf | grep vg0 | awk '{ print $5"_"$6}'`
do
if [[ $a != "_" ]]
then
b=`echo $a | cut -d"%" -f1`
if [[ $b -gt $u ]]
then
c=`echo $a | cut -d"_" -f2`
#echo $c ${b}% >> $log/`echo $0 cut -d"." -f1`/filesystem_high_$hour
echo $c ${b}% >> ${LOG}/filesystem_high_$hour
zz=1
mailx -s "WARNING on $(hostname) - Filesystem size higher than ${1}%" $emails_dma < ${log}/filesystem_hight_$hour
echo "FS size > ${1}%" | mailx -s "WARNING on $(hostname) - " $cellulars_dma
fi
fi
done


Manuales
Super Advisor

Re: How to assign a variable containing other variables

HIIIIII
I know what is the problem:

LOG=$($log/$(echo $0 | cut -d"/" -f5 | cut -d"." -f1))

i have to writte the following:
LOG="$log/$(echo $0 | cut -d"/" -f5 | cut -d"." -f1)"

Thanks anyway ..
Hein van den Heuvel
Honored Contributor
Solution

Re: How to assign a variable containing other variables

Yeah, but there is a bigger problem really.
That manual parsing of the scrip name is just horrible.
Very inflexible. What if the script is supposed to have a . in the middle of the name?
What if you stick in one directory level deeper or less deep?


Please consider using 'basename'

for example:

LOG="$log/$(basename $0 .sh)

hth,
Hein.


Hein van den Heuvel
Honored Contributor

Re: How to assign a variable containing other variables

Ooops, sorry for the sloppy typing.

more free advice....

1) you don't need those double quotes to build $LOG.
They don't hurt, but is help you in the long run to know when they are needed, when not. By not having them you can not accidently get them out of balance as i did by (cut & paste) accident in the earlier reply

2) be sure to check out the shell build-ins for variable parsing. In this case consider:

NAME=${0##*/}
STRIPPED=${NAME%.*}
echo "name:$NAME stripped:$STRIPPED"

3) Google: basename +site:itrc.hp.com


Cheers,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: How to assign a variable containing other variables

Hi Manuales:

As Hein noted, consider either using 'basename' and 'dirname' or use the Posix shell's pattern substitution.

Have a look at the manpages for 'basename(1)' [which encompases a discussion of 'dirname(1) too]:

http://docs.hp.com/en/B2355-60105/basename.1.html

Notice that there is an optional 'suffix' argument to 'basename' which allows you to return only the basename without any "extension". For example:

# F=/path/abc.sh
# echo `dirname ${F}`
/path
# echo `basename ${F}`
abc.sh
# echo `basenane ${F} .sh`
abc

Having the ability to snip off the basename without an extension is useful for manipulating related files as in a directory of "*.c" and "*.o" files for example.

For speed, you can often use the shell build-in parameter substitution:

${parameter#pattern}
${parameter##pattern}
${parameter%pattern}
${parameter%%pattern

This is described under "Parameter Substitution" in the 'sh-posix(1)' manpages:

http://docs.hp.com/en/B2355-60105/sh-posix.1.html

# F=/path/to/name;echo ${F%/*}
/path/to
# F=/path/to/name;echo ${F##*/}
name

Regards!

...JRF...