Operating System - HP-UX
1828409 Members
3549 Online
109977 Solutions
New Discussion

Building an environment file

 
SOLVED
Go to solution
Manuales
Super Advisor

Building an environment file

Hi, i'm biulding an environment file named:
.environment

and it contains the following:
dirs="scripts scripts-log menu monitoring processes functions scripts main-log"
for minuscula in `echo $dirs`
do
mayuscula=`echo $minuscula | tr "[:lower:]" "[:upper:]"`
if [[ $minuscula = "scripts-log" ]]
then
${minuscula}="${HOME}/SCRIPTS/${1}/LOG"
else
${minuscula}="${HOME}/$mayuscula"
fi
done
mails="dm@yahoo.com.mx hola@yahoo.com.mx"
hn-server=$(hostname)


but the output the following is:
$ . ./.environment 0001-Alert-Temperature
ksh: scripts=/home/DRTEAM01/SCRIPTS: not found
ksh: scripts-log=/home/DRTEAM01/SCRIPTS/0001-Alert-Temperature/LOG: not found
ksh: menu=/home/DRTEAM01/MENU: not found
ksh: monitoring=/home/DRTEAM01/MONITORING: not found
ksh: processes=/home/DRTEAM01/PROCESSES: not found
ksh: functions=/home/DRTEAM01/FUNCTIONS: not found
ksh: scripts=/home/DRTEAM01/SCRIPTS: not found
ksh: main-log=/home/DRTEAM01/MAIN-LOG: not found
ksh: hn-server=torpdbp: not found

Why does appears that mistake?
it supposes that it must shows the assigned, does not it?

Please help.
Thanks.
4 REPLIES 4
Bill Hassell
Honored Contributor

Re: Building an environment file

The lines:

${minuscula}="${HOME}/SCRIPTS/${1}/LOG"

${minuscula}="${HOME}/$mayuscula"

are incorrect. Assignments for variable must list the variable name, not the contents. When you put $ or ${} around a variable name, the shell replaces the variable name with the contents of the variable. The lines should read:

minuscula="${HOME}/SCRIPTS/${1}/LOG"

minuscula="${HOME}/$mayuscula"


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: Building an environment file

Hi,

in addition to Bills correct solution:
Having set
dirs="scripts scripts-log menu monitoring processes functions scripts main-log"

... there is no need to use such a construction:

for minuscula in `echo $dirs`
do ...
done

Better and simpler just use
for minuscula in $dirs
do ...
done

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Manuales
Super Advisor

Re: Building an environment file

Hi ..
but i need to put $minusucula because every time it must be:

scripts="${HOME}/SCRIPTS"
scriptslog="${HOME}/SCRIPTS/${1}/LOG"
menu="${HOME}/MENU"
monitoring="${HOME}/MONITORING"
processes="${HOME}/PROCESSES"
functions="${HOME}/FUNCTIONS"
scripts="${HOME}/SCRIPTS"
mainlog="${HOME}/MAINLOG"

it's because the contents of the variable is the value of "dirs" ....

i can not put :
minuscula="${HOME}/$mayuscula"

i need to put
$minuscula="${HOME}/$mayuscula"

where the first time minuscula would be:
minuscula=scripts
scripts=/home/DRTEAM01/SCRIPTS

minuscula=scriptslog
scriptslog=/home/DRTEAM01/SCRIPTS//LOG

minuscula=menu
menu=/home/DRTEAM01/MENU

and go on ....

what could i built this lines???

Regards.
Peter Nikitka
Honored Contributor
Solution

Re: Building an environment file

Hi,

to get a variable name depending on a variable, you could use

for minuscula in $dir
do
eval $minuscula=$dir
...
done

BUT: this will NOT work for the values
dir=main-log
dir=hn-server
since a variable name cannot contain a dash '-'.

Better use arrays and access the pairs of the requested values through equal indices.
Example:

set -A dir scripts scripts-log menu monitoring processes functions scripts main-log
typeset -i idx=0 asize=0

set -A mayuscula $(typeset -u mayus
for mm in ${dir[*]}
do
mayus=$mm
if [[ $mm = "scripts-log" ]]
then print ${HOME}/SCRIPTS/${1}/LOG
else print ${HOME}/$mayus
fi
done
)

asize=${#dir[*]}
while [ idx -lt asize ]
do
print dir=${dir[idx]} mayuscula=${mayuscula[idx]}
((idx+=1))
done

The latter is just an output of the coresponding array values - put your code here.

mfG Peter
PTW: the code is untested ...
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"