I am assigning a variable name to --> $user_var
Then I am trying to change its underlying variable value by
$((user_var))=$user_value
.. its failing,,
Please let me know if there is a way to do this dynamically..
FileA.props -->
myvar1=test1 # Variable Names and values can be changed.
myvar2=test2
myvar3=test3
# EOF
testscript.sh -->
#!/usr/bin/ksh
# Helps to change values using this menu driven script
cvsprops=FileA.props
# Executing include file, to simulate variable declaration.
# This is a must,, since its a parameter driven script.
. FileA.props
user_prompt='To Change Above Defaults Make A Selection:[n] '
PS3="${user_prompt}"
REDISPLAY=true # Helps to exit from Menu.
while [ "$REDISPLAY" == "true" ]; do
select line in $(egrep -v "#" $cvsprops ) REVIEW DONE
do
[ "$line" == "DONE" ] && REDISPLAY=false && break
[ "$line" == "REVIEW" ] && break
[ ! -n "$line" ] && echo " Invalid choice:[$REPLY]. Please make correct choice." && continue
user_var="$(echo $line | cut -d= -f1)"
user_value="$(echo $line | cut -d= -f2)"
#echo "Length of $user_value --> ${#user_value}"
# if required variable is not set. Prompt user to set it.
if [ ! -n "$user_value" ]; then
echo "Set --> ${user_var} ?: \c"
else
echo "Change --> ${user_var}:[${user_value}] ?: \c"
fi
read user_value
if [ -n $user_value ]; then
echo "new value for ${user_var} --> $user_value"
# *** This is failing.. How can I change say
# myvar1 value to 'mytest4' !!?
$((user_var))=$user_value
fi
done # End of - select
done # End of while
# EOF
$>testscript.sh
......< Menu > .........
To Change Above Defaults Make A Selection:[n] 4
Set --> cvsRepositoryUserID ?: n
new value for myvar1 --> n
testscript.sh[]: myvar1: bad number
Thanks for the response.
While your suggested code is not failing,,its not solving the problem either.
read user_value
if [ -n $user_value ]; then
echo "new value for ${user_var} --> $user_value"
# *** This is failing.. How can I change say
# myvar1 value to 'mytest4' !!?
eval "$user_var=$user_value"
fi
When I display variable values,, they are still pointing to old values.
Output --->
icoe12:/export/apps/rh50ip2/Scripts/opsUtils/cvstools/junk/test> testscript.sh
1) myvar1=test1
2) myvar2=test2
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 2
Change --> myvar2:[test2] ?: test4
new value for myvar2 --> test4
To Change Above Defaults Make A Selection:[n] 4
1) myvar1=test1
2) myvar2=test2
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n]
I would appreciate if someone tries my code on their system and get it working..
FileA.props -->
# Variable Names and values can be changed.
myvar1=test1
myvar2=test2
myvar3=test3
# EOF
I tested your script and it works fine.
The thing is that you display the content of the file (by selecting REVIEW), not the values of variables.
Do you want the script to modify the values in FileA.props?
else, you'll have to modify the select statement.
#!/usr/bin/ksh
#==============================================================================
# F U N C T I O N S . . .
#==============================================================================
ReadProps () {
local list
list=$(sed 's/#.*//' ${cvsprops} | \
awk -F= '
NF { count++;
printf "Uvar[%d]=%s; Uval[%d]=%s;", count, $1, count, $2;
printf "Uchoice[%d]=%s;", count, $0;
}
END { printf "Ucount=%d", count }'
)
eval ${list}
}
WriteProps () {
local i var val
i=1
while (( i <= Ucount ))
do
var=${Uvar[$i]}
val=${Uval[$i]}
echo "${var}='${val}'"
(( i+=1 ))
done > ${cvsprops}
}
#==============================================================================
# M A I N . . .
#==============================================================================
# Helps to change values using this menu driven script
cvsprops=FileA.props
user_prompt='To Change Above Defaults Make A Selection:[n] '
PS3="${user_prompt}"
REDISPLAY=true # Helps to exit from Menu.
while [ "$REDISPLAY" = "true" ]
do
ReadProps
select line in "${Uchoice[@]}" REVIEW DONE
do
[ "$line" = "DONE" ] && REDISPLAY=false && break
[ "$line" = "REVIEW" ] && break
[ -z "$line" ] && echo " Invalid choice:[$REPLY]. Please make correct choice." && continue
user_var=${Uvar[$REPLY]}
user_value="${Uval[$REPLY]}"
# if required variable is not set. Prompt user to set it.
if [ -z "$user_value" ]; then
echo "Set --> ${user_var} ?: \c"
else
echo "Change --> ${user_var}:[${user_value}] ?: \c"
fi
read user_value
if [ -n "$user_value" ]; then
Uval[$REPLY]="${user_value}"
eval ${user_var}='${user_value}'
WriteProps
eval user_value=\$${user_var}
echo "new value for ${user_var} --> $user_value"
break
fi
done # End of - select
done # End of while
# EOF
Input file FileA.props :
# Variable Names and values can be changed.
myvar1=test1
myvar2=test2
myvar3=test3
# EOF
Sample execution :
$ kc.sh
1) myvar1=test1
2) myvar2=test2
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 1
Change --> myvar1:[test1] ?: First test
new value for myvar1 --> First test
1) myvar1=First test
2) myvar2=test2
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 1
Change --> myvar1:[First test] ?:
To Change Above Defaults Make A Selection:[n] 2
Change --> myvar2:[test2] ?: 2222
new value for myvar2 --> 2222
1) myvar1=First test
2) myvar2=2222
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 1
Change --> myvar1:[First test] ?: 1111
new value for myvar1 --> 1111
1) myvar1=1111
2) myvar2=2222
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 5
$ sh kc.sh
1) myvar1=1111
2) myvar2=2222
3) myvar3=test3
4) REVIEW
5) DONE
To Change Above Defaults Make A Selection:[n] 5
$