Assigning values to reference variables for a dynamic menu driven script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning values to reference variables for a dynamic menu driven script.
# 1  
Old 04-29-2010
Question Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables?

I am assigning a variable name to --> $user_var

Then I am trying to change its underlying variable value by
Code:
$((user_var))=$user_value

.. its failing,,

Please let me know if there is a way to do this dynamically..

FileA.props -->
Code:
myvar1=test1   #  Variable Names and values can be changed.
myvar2=test2
myvar3=test3
# EOF

testscript.sh -->
Code:
#!/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

Code:
$>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


Last edited by Franklin52; 04-29-2010 at 06:36 PM.. Reason: Please use code tags!
# 2  
Old 04-30-2010
Code:
$((user_var))=$user_value

That is arithmetic
try:
Code:
eval "$user_var=$user_value"

# 3  
Old 04-30-2010
Thanks for the response.
While your suggested code is not failing,,its not solving the problem either.
Code:
        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 --->
Code:
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 -->

Code:
#  Variable Names and values can be changed.
myvar1=test1   
myvar2=test2
myvar3=test3
# EOF

# 4  
Old 04-30-2010
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.

The REDISPLAY variable seems useless.
# 5  
Old 04-30-2010
A possible solution...
Try and adapt :
Code:
#!/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 :
Code:
#  Variable Names and values can be changed.
myvar1=test1   
myvar2=test2
myvar3=test3
# EOF

Sample execution :
Code:
$ 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
$

Jean-Pierre.
# 6  
Old 04-30-2010
Smilie Jean-Pierre, Thanks a lot for taking time to solve it.. It works great..
SmilieSmilie SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using menu driven script

Hi Team , I wrote a shell script for adding and subtracting two numbers am getting error could some one please help to fix it script: echo "Enter 1 to add:" echo "Enter 2 to sub:" echo "Enter 3 for both addition and subtraction :" read ans; case "$ans" in 1)... (4 Replies)
Discussion started by: knz
4 Replies

2. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

3. Shell Programming and Scripting

Use of stty vs trap in script-driven login menu

My employers would like me to selectively run one of several different (already-existing) Korn Shell menu-driven scripts out of the user's .profile file, depending on some yet-to-be-specified user critieria. I've never done this kind of thing, but I have the existing scripts (among other... (5 Replies)
Discussion started by: Clovis_Sangrail
5 Replies

4. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

5. Shell Programming and Scripting

Menu driven script.

I'm a beginner at scripting and have been putting this script together over the past week. It's no where as polish as it could be. Any tips/suggestions on improving this script would be appreciate it. Every week, my team develops WAR files in tomcat on our test environment and moves them to our... (4 Replies)
Discussion started by: bouncer
4 Replies

6. Shell Programming and Scripting

Help needed in writing a menu driven script

Hi, I was wondering if someone could help me write a shell script in Linux that backsup/restores data to anywhere I choose but it needs to be menu driven? Thanks, I'm new to Linux/Unix but liking it so far...just hoping to get to grips with the scripts! :) (7 Replies)
Discussion started by: Nicole
7 Replies

7. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

8. Shell Programming and Scripting

Menu driven Script needed ..pls help

Hi Guys.. am new to unix scrpiting..I need a Menu need to create using shell scrpting eg: Food items ready paid if i press "f" need to add items for a file food items.. if i press "r" it need to move into ready and remove from food items if i press "p" need to update a filed in... (1 Reply)
Discussion started by: sasdua
1 Replies

9. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

10. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies
Login or Register to Ask a Question