Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-25
15
Medium Priority
?
770 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 85

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 20

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 20

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 20

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
 

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 20

Accepted Solution

by:
simon3270 earned 2000 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 20

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 20

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

885 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