Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

borne shell x=y, $x=7 echo $y, not set

Status
Not open for further replies.

mcrobiej

Programmer
Feb 27, 2002
4
0
0
US
Ok this must be a quick one to answer

borne shell script:

x=y
$x=7

error
y=7

, how do i use the content of a variable as a name for a new variable?

also how do i return parameters from a function

i.e.

_read()
{
prompt=$1
temp=""

valid=0
while [ "$temp" = "" ]
do
echo "$1 : \c"
read temp
done
}

### main

option=_read "please enter your choice"

#?
 
x=7
y=$x
echo $x
7
echo $y
7

======================
Read() {
echo $1
read fooo
}
Read "This is a test"
echo $fooo



Bill.
 
Bill, not quite,

you have turned it around to make it work !!

let me add more info

variablename=mode

now I want to set the variable mode to manual, but this does not work.

$variablename=manual

. You also have done the function returning using a global variable, I already have it working this way but it is messy.

_read()
{
prompt=$1
temp=""

valid=0
while [ "$temp" = "" ]
do
echo "$1 : \c"
read temp
done
}

####

_read "> Function (start/stop/exit)"
operation=$temp
_read "> Mode(Manual/automatic)"
mode=$temp


#####

there must be a nicer way? I want to pass a variable back,




 
You'll be hard pressed to do the first stop of this. As for the second step, the only way to do it is to set a global variable to the value of a private variable. That is by definition what a private variable is.

Bill.
 
I have only been able to do this by using an array.

Here's an example.

You could echo your dynamic names and redirect
them into the TMP_LIST file.
You could then read them from the array
and set values for them.


#!/usr/bin/ksh
TMP_LIST=/tmp/TMP_LIST.$$ # Create_Array reads from this file to create array

Create_Array () # requires one argument for ARRAY_NAME and reads from TMP_LIST
{
ARRAY_NAME=$1
ITEM_NUMBER=1 # start item number at 1
cat ${TMP_LIST}.${ARRAY_NAME}|while read ARRAY_ITEM
do
echo "...\c"
eval "${ARRAY_NAME}[${ITEM_NUMBER}]=\"${ARRAY_ITEM}\"" # read from $TMP_LIST and add ite
m to array
ITEM_NUMBER=`expr $ITEM_NUMBER + 1` # increment item number by one
done
> $TMP_LIST.$ARRAY_NAME # clear TMP_LIST
# > ARRAY_ITEM
}

Display_Array () # list contents of the array on screen - this can be used in conjunction with | or >>
{
ARRAY_NAME=$1
DISPLAY_COUNTER=1
ARRAY_STRING="\${#$ARRAY_NAME[*]}"
eval "ARRAY_ITEM_COUNT=$ARRAY_STRING"

until [ $DISPLAY_COUNTER -eq $ARRAY_ITEM_COUNT ] # print array
do
ARRAY_STRING="\${$ARRAY_NAME[$DISPLAY_COUNTER]}"
eval "ARRAY_ITEM=$ARRAY_STRING"
echo $ARRAY_ITEM
DISPLAY_COUNTER=`expr $DISPLAY_COUNTER + 1`
done
}

Example:
ps -ef >> $TMP_LIST

Create_Array ps

Display_Array ps

for your progam just substitute the ps -ef
with whatever method you are using to
create the dynamic variable names.

Robert

Robert G. Jordan

Robert@JORDAN2000.com
Unix Admin, United Airlines
 
I think you want a reference to a variable onto another variable... try
[tt]
${$var}
[tt]
With the name inside $var variable.
I hope it works...
Unix was made by and for smart people.
 
EglisRamon, if you can get this to work,
that would be great.I've always been
stumped with this issue.

Robert

Here's script I wrote for a test
and the ouput is listed below.

#!/usr/bin/sh
# virtual_var script
# attempt to create a virtual variable

VARIABLE_NAME=MODE

# ${$var}

${$VARIABLE_NAME}=manual # this command doesn't seem to work

echo "Variable Name: $VARIABLE_NAME"
# output I'm hoping for: Variable Name: MODE

echo "$VARIABLE_NAME = ${$VARIABLE_NAME}"
# output I'm hoping for: MODE = manual

----OUTPUT--------------------
# ./virtual_var
./virtual_var[7]: ${$VARIABLE_NAME}=manual: The specified substitution is not valid for this command. Robert G. Jordan

Robert@JORDAN2000.com
Unix Admin, United Airlines
 
hi,
part of the job could be done using 'eval'
in Bourne Shell:
x=y
eval $x=7
So far it will work; when you type 'echo $y', you will get '7'. But i do not know how to replace 'echo $y' by something containing '$x'.
Someone else has an idea?
regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top