Sponsored Content
Top Forums Shell Programming and Scripting Assigning values to reference variables for a dynamic menu driven script. Post 302417696 by aigles on Friday 30th of April 2010 09:30:00 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 03:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy