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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic case

Status
Not open for further replies.

hok1man

Technical User
Feb 16, 2008
102
Hi guys,

I was wondering whether we can do dynamic in case options from a file :

for example in a file content like this :
step_first.ksh
step_second.ksh
step_third.ksh


so in normal static case selection it would look like this.
Code:
case "$SELECT" in
1) step_first.ksh ;;
2) step_second.ksh ;;
3) step_third.ksh ;;
esac

how if I would like to make it dynamic, so if I put step_four.ksh in the file, I don't have to change my script.

Any idea would be appreciated.

Thanks guys
 
Here's an idea for a script:
- read the file of 'script-names'
- create the commands for the case statement and write them to a file in /tmp (as a script)
- run the /tmp script (which prompts for $SELECT and runs the chosen named script)
- do any further processing required
- delete the /tmp/script
- exit

The only file that will need updating is the file containing the 'script-names'


I hope that helps, and makes sense.

Mike
 
If you file is called [tt]scripts.dat[/tt]...
Code:
step_first.ksh
step_second.ksh
step_third.ksh
...you can do this...
Code:
#!/bin/ksh

SELECT=2

print $SELECT | select CHOICE in $(<scripts.dat)
do
    print "You selected $CHOICE"
    break
done
Then, all you do is add new options to [tt]scripts.dat[/tt] and they will be automatically added to the options you can select from.

If you want it to be interactive and have someone type the option, it's like this...
Code:
#!/bin/ksh

select CHOICE in $(<scripts.dat)
do
    print "You selected $CHOICE"
    break
done
You can fine tune and tweak for your particular problem.

 
The following code snippet is probably a bit over the top for what you want but it's a general purpose menu using ksh arrays
Code:
do_menu()
  {
  while :
  do
    # Clear the screen
    tput clear
    # Display the header
    echo "\n\n\t$(tput smso)$header$(tput rmso)\n"
    # Display any error message
    [[ -n "$message" ]] && { echo "\t$message\n"; message=; }
    # Display menu lines
    IDX=1
    while [ $IDX -lt ${#contents[*]} ]
    do
      printf "\t%2.2s - %s\n" $IDX "${contents[$IDX]}"
      (( IDX += 1 ))
    done
    echo "\n\t Q - Quit\n\n\t\c"
    # Read reply
    read ans
    # Handle blank answer
    [[ -z "$ans" ]] && {
      message="No response given"
      continue
      }
    # If ans == 'q' then quit
    [[ $ans = 'q' || $ans = "Q" ]] && exit
    # Check the answer is pure numeric
    expr $ans : "^[0-9]*$" > /dev/null || {
      message="Please enter numbers only"
      continue
      }
    # Check answer is within range
    [[ $ans -lt 1 || $ans -ge ${#contents[*]} ]] && {
      message="$ans is not a valid response"
      continue
       }
    # All tests passed - return ans
    return $ans
  done
  }
Before calling it you would have something along the lines of
Code:
IFS='
'
header="This is a menu header"
set -A contents "" "Selection Choice 1" "Selection Choice 2"
do_menu
So, if your selections are in a file you would use
Code:
IFS='
'
header="This is a menu header"
set -A contents "" $(< /path/to/file/with/selections)
Things to note are[ul]
[li]The first entry in the array is a null string. This is to get round the fact that ksh arrays start at 0 whereas user friendly menus start at 1[/li]
[li]The IFS has to be set to CR to prevent spaces in the menu choices being seen as record seperators[/li]
[li]The choice is returned as an error value[/li][/ul]



On the internet no one knows you're a dog

Columb Healy
 
Hi Sambones,

Unfortunately there are some scripts we need to sudo, so in the script.dat would be :

step_first.ksh
step_second.ksh
sudo su - appadm -c "step_third.ksh"

after I put in select, they divided word by word, so it becomes

Code:
 1) sudo
 2) su
 3) -
 4) appadm
 5) -c
 6) "step_third.ksh"

how do I parse it by line ?

Thanks mate
 
You may try this:
Code:
#!/bin/ksh
[!]IFS="
"[/!]
select CHOICE in $(<scripts.dat)
do
    print "You selected $CHOICE"
    break
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top