Solved

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

Posted on 2013-11-25
15
726 Views
Last Modified: 2013-11-29
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
Comment
Question by:tindavid
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39676738
The line

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

Should be doing that.
0
 

Author Comment

by:tindavid
ID: 39676875
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 39676905
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
 
LVL 84

Expert Comment

by:ozo
ID: 39676916
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
 
LVL 19

Expert Comment

by:simon3270
ID: 39676983
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
 
LVL 19

Expert Comment

by:simon3270
ID: 39677000
(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
 
LVL 19

Expert Comment

by:simon3270
ID: 39677213
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:tindavid
ID: 39679675
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
 

Author Comment

by:tindavid
ID: 39679676
Hi Simon, space handling of the Menu option is required, please cater it if you can.
0
 

Author Comment

by:tindavid
ID: 39679724
Simon,  code of   local prms=("$@")    changed to " set -A prms $@"  and works.  Now, please help to solve the problem of Space Menu titile
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39680180
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
 

Author Closing Comment

by:tindavid
ID: 39682481
perfect , also solved the issue of space in the menu title.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39683024
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
 

Author Comment

by:tindavid
ID: 39684854
overlooked, am I too late ?
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39684902
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now