Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

How to inlcude the main title in k or c shell script

Here is the script to repeatly display a memu

#!/usr/bin/ksh

mtitle="       Welcome to mymenu for :${user}@${server}
        [ Running @${hostname} with OS:${os} ${bits}bit ]"
echo " "
echo "${mtitle}"
echo " "
PS3="Select Performance Indicator Option."${main_ps3}
echo " "
select envt in "Segment Monitor" "Lock / Wait Monitor" "Blocker" "Session Monitor" "System Monitor" "Unix Utilities" "Quit
"
do
  case "${envt}" in
       "Segment Monitor") opt=${envt}; clear; echo "${mtitle}"; mu_monitor_segment; clear; echo "${mtitle}";;
       "Lock / Wait Monitor") opt=${envt}; clear; echo "${mtitle}"; mu_monitor_lock_wait; clear; echo "${mtitle}";;
       "Blocker") opt=${envt}; clear; echo "${mtitle}"; check_blocker; clear; echo "${mtitle}";;
       "Session Monitor") opt=${envt}; clear; echo "${mtitle}"; mu_monitor_session; clear; echo "${mtitle}";;
       "System Monitor") opt=${envt}; clear; echo "${mtitle}"; mu_monitor_system; clear; echo "${mtitle}";;
       "Unix Utilities") opt=${envt}; clear; echo "${mtitle}"; mu_unix_utl; clear; echo "${mtitle}";;
       "Quit") exit_program; clear; echo "${mtitle}";;
       *)         echo "Invalid option"; clear; echo "${mtitle}";;
  esac
done


All I want is to have the main title ($mtitle) be display whenever I have press a return without any selection, the mtitle will display
0
tindavid
Asked:
tindavid
  • 6
  • 6
  • 2
  • +1
1 Solution
 
omarfaridCommented:
The line

 *)         echo "Invalid option"; clear; echo "${mtitle}";;

Should be doing that.
0
 
tindavidAuthor Commented:
The solution of

 *)         echo "Invalid option"; clear; echo "${mtitle}";;

is ready in my code, and it is not working, that is why I am putting there for someone to correct it.
0
 
omarfaridCommented:
You may try code below if it works, and then you can put required actions for each option

#!/usr/bin/ksh
while true
do
      mtitle="       Welcome to mymenu for :${user}@${server}
        [ Running @${hostname} with OS:${os} ${bits}bit ]"
        echo " "
        echo "${mtitle}"
        echo " "
       echo "1- Segment Monitor
                  2- Lock / Wait Monitor
                  3- Blocker
                  4- Session Monitor
                  5- System Monitor
                  6- Unix Utilities
                  7- Quit
                   
                  Select Performance Indicator Option: " ; read ans

  case $ans in
       1)
           ;;
        2)
           ;;
        3)
           ;;
        4)
           ;;
       5)
           ;;
        6)
           ;;
        7)
           ;;
        *)         echo "Invalid option"; clear; echo "${mtitle}";;
  esac
done
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ozoCommented:
Do you want to set
PS3="${mtitle}
Select Performance Indicator Option."${main_ps3}
?
Do you want
"Quit
")
or
"Quit"*)
instead of
"Quit")
?
In what way does your code not work as you intended, and what did you want it to do differently?
0
 
simon3270Commented:
The problem is that if you just enter a blank line, you stay within the "select" call, so it doesn't go through the case statement code until you enter a valid number.

One fix is to write your own version of select which *does* return that blank entry.  For example, add this early in your script:
function myselect()
{
  local var=$1
  shift 2
  local prms=($@)
  while true; do
    local n=0
    local ans
    local sel
    for ii in ${prms[@]}; do
      ((n=n+1))
      echo "${n}) $ii"
    done
    printf "%s" "$PS3"
    read ans
    if [ $? -ne 0 ]; then
      return 1
    fi
    if [ "$ans" = "" ]; then
      eval "${var}=${ans}"
      break
    fi
    if expr "$ans" + 0 >&/dev/null; then
      if [ $ans -gt 0 -a $ans -le $n ]; then
        ((sel=ans-1))
        eval "${var}=${prms[$sel]}"
        break
      fi
    fi
  done
  eval "REPLY=${ans}"
  return 0
}

Open in new window

then replace your "select" line with:
while myselect envt in "Segment Monitor" "Lock / Wait Monitor" "Blocker" "Session Monitor" "System Monitor" "Unix Utilities" "Quit"

Open in new window

(i.e. replace "select" at the start of the line with "while myselect")
0
 
simon3270Commented:
(Ah, my function doesn't cope well with arguments with spaces in them - for now you could replace them with underscores, so have "Segment_Monitor")
0
 
simon3270Commented:
I'd missed a couple of sets of double quotes.  Corrected version is
function myselect()
{
  local var=$1
  shift 2
  local prms=("$@")
  while true; do
    local n=0
    local ans
    local sel
    for ii in "${prms[@]}"; do
      ((n=n+1))
      echo "${n}) $ii"
    done
    printf "%s" "$PS3"
    read ans
    if [ $? -ne 0 ]; then
      return 1
    fi
    if [ "$ans" = "" ]; then
      eval "${var}=${ans}"
      break
    fi
    if expr "$ans" + 0 >&/dev/null; then
      if [ $ans -gt 0 -a $ans -le $n ]; then
        ((sel=ans-1))
        eval "${var}=\"${prms[$sel]}\""
        break
      fi
    fi
  done
  eval "REPLY=${ans}"
  return 0
}

Open in new window

0
 
tindavidAuthor Commented:
Omarfarid's suggestion is simple to use but requires more change of my coding as I use many menus in my program. Credit will be given

Simon's suggestion is more applicable to mine requirement as less change to my code. But an error is found whith the code: local prms=("$@")

if the brackets are removed, menus cannot be displayed properly. see attached code.
mycode.txt
0
 
tindavidAuthor Commented:
Hi Simon, space handling of the Menu option is required, please cater it if you can.
0
 
tindavidAuthor Commented:
Simon,  code of   local prms=("$@")    changed to " set -A prms $@"  and works.  Now, please help to solve the problem of Space Menu titile
0
 
simon3270Commented:
To get spaces to work, you need double quotes round the $@, so
    set -A prms "$@"

Since this is for ksh, I've reworked the function, taken out bash-specific calls (the ">&", for example), used typeset rather than local, and changed the function definition line to "function myselect {" so that typeset variables are local.  It is now:
function myselect {
  typeset ans=""
  typeset -i sel=0
  typeset -i n=0

  typeset var=$1
  shift 2

  set -A prms "$@"

  while true; do
    n=0
    ans=""
    sel=0
    for ii in "${prms[@]}"; do
      ((n=n+1))
      echo "${n}) $ii"
    done
    printf "%s" "$PS3"
    read ans
    if [ $? -ne 0 ]; then
      echo
      return 1
    fi
    if [ "$ans" = "" ]; then
      eval "${var}=${ans}"
      break
    fi
    if expr "$ans" + 0 >/dev/null 2>&1; then
      if [ $ans -gt 0 -a $ans -le $n ]; then
        ((sel=ans-1))
        eval "${var}=\"${prms[$sel]}\""
        break
      fi
    fi
  done
  eval "REPLY=${ans}"
  return 0
}

Open in new window

0
 
tindavidAuthor Commented:
perfect , also solved the issue of space in the menu title.
0
 
simon3270Commented:
Thanks for the points (though I thought you were going to give omarfarid some too).

As a bonus, here's a version of myselect which works unchanged in Bash and Ksh:
function myselect {
  typeset ans=""
  typeset prms
  typeset ii
  typeset -i sel=0
  typeset -i n=0

  typeset var=$1
  shift 2

  for ii in "$@"; do
    prms[$n]="$ii"
    ((n=n+1))
  done

  while true; do
    n=0
    ans=""
    sel=0
    for ii in "${prms[@]}"; do
      ((n=n+1))
      echo "${n}) $ii"
    done
    printf "%s" "$PS3"
    read ans
    if [ $? -ne 0 ]; then
      echo
      return 1
    fi
    if [ "$ans" = "" ]; then
      eval "${var}=${ans}"
      break
    fi
    if expr "$ans" + 0 >/dev/null 2>&1; then
      if [ $ans -gt 0 -a $ans -le $n ]; then
        ((sel=ans-1))
        eval "${var}=\"${prms[$sel]}\""
        break
      fi
    fi
  done
  eval "REPLY=${ans}"
  return 0
}

Open in new window

(it builds up the prms array element by element, and adds localisation of the ii and prms variables)
0
 
tindavidAuthor Commented:
overlooked, am I too late ?
0
 
simon3270Commented:
It's never too late!  You should have a little yellow "Request Attention" icon (down at the bottom of the question?) - click on that, say that you want to reassign points, and a moderator will reopen the question.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now