PS3 and SELECT, is it possible to put a line break?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PS3 and SELECT, is it possible to put a line break?
# 1  
Old 09-11-2012
PS3 and SELECT, is it possible to put a line break?

Hi all,

Before I give up on using SELECT for my first attempt at creating a menu driven script, can anyone please advise if it is possible to include a line break for PS3, I've tried putting in a \n and it does not work.

Tried for both bash and ksh and both gives the same result. Preference is to use ksh.

Code:
 
menu_main1()
{
   PS3="\n ::  Please choose a VALID option : "
   echo ""
   echo ""
   select choice_main in "Generic SA" "Refresh Tasks" "Others" "Exit"
   do
      case $choice_main in
         "Generic SA" )
            echo "Generic SA"
            clear
            menu_db_admin
            ;;
         "Refresh Tasks" )
            echo "Refresh Tasks"
            ;;
         "Others" )
            echo "Others"
            ;;
         "Exit" )
            exit 0
            ;;
      esac
   done
}

Output is as below:

Code:
 

 
1) Generic SA
2) Refresh Tasks
3) Others
4) Exit
\n ::  Please choose a VALID option :

If someone believes there is a better alternative to using select, formatting wise, please advise. An example may be best if any. I've seen heaps of examples in Google but maybe someone has a proven way of making a menu driven script.

Thanks in advance.
# 2  
Old 09-12-2012
The Korn shell allows literal <newline> characters in quoted strings and has a $'...' form to allow the C language's backslash escapes in strings. So either of the following will set PS3 the way you want it:
Code:
PS3="
 ::  Please choose a VALID option : "

or
Code:
PS3=$'\n ::  Please choose a VALID option : '

The rest of your script looks OK to me (although I don't know what menu_db_admin is supposed to do).

When I'm writing ksh scripts, I usually add the optional opening parentheses in the case clauses to I can more easily match opening and closing braces, parentheses, and square brackets when I'm editing scripts.

Since you have this menu inside a shell function, I would usually get out of the function with a return rather than an exit, but that clearly depends on what you want to do with this function.
Code:
#!/bin/ksh
menu_main1() {
        PS3=$'\n ::  Please choose a VALID option : '
        printf "\n\n"
        select choice_main in "Generic SA" "Refresh Tasks" "Others" "Return"
        do
                case $choice_main in
                ( "Generic SA" )
                        echo "Generic SA"
                        clear
                        menu_db_admin;;
                ( "Refresh Tasks" )
                        echo "Refresh Tasks";;
                ( "Others" )
                        echo "Others";;
                ( "Return" )
                        return;;
                esac
        done
}
echo about to call menu_main1
menu_main1
echo menu_main1 has returned

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-13-2012
Quote:
Originally Posted by Don Cragun
The Korn shell allows literal <newline> characters in quoted strings and has a $'...' form to allow the C language's backslash escapes in strings. So either of the following will set PS3 the way you want it:
Code:
PS3="
 ::  Please choose a VALID option : "

or
Code:
PS3=$'\n ::  Please choose a VALID option : '

The rest of your script looks OK to me (although I don't know what menu_db_admin is supposed to do).

When I'm writing ksh scripts, I usually add the optional opening parentheses in the case clauses to I can more easily match opening and closing braces, parentheses, and square brackets when I'm editing scripts.

Since you have this menu inside a shell function, I would usually get out of the function with a return rather than an exit, but that clearly depends on what you want to do with this function.
Code:
#!/bin/ksh
menu_main1() {
        PS3=$'\n ::  Please choose a VALID option : '
        printf "\n\n"
        select choice_main in "Generic SA" "Refresh Tasks" "Others" "Return"
        do
                case $choice_main in
                ( "Generic SA" )
                        echo "Generic SA"
                        clear
                        menu_db_admin;;
                ( "Refresh Tasks" )
                        echo "Refresh Tasks";;
                ( "Others" )
                        echo "Others";;
                ( "Return" )
                        return;;
                esac
        done
}
echo about to call menu_main1
menu_main1
echo menu_main1 has returned


Hi Don,

Thanks for your reply. I have to use your first suggestion, why didn't I think of that Smilie Reason being is that your second suggestion does not work on PDKsh and KSH88 Smilie ...I tried it out on the 3 servers that I need to run the script for and the second suggestion you mentioned only works for KSH93.

Good idea on using the return instead of exit. Just to clarify, the return will always return to the calling sub, correct?

BTW, I also just discovered that if I have more than 4 select options to choose, it wraps into the next column, is there any way to control this behavior?

Thanks again for reply.
# 4  
Old 09-13-2012
Quote:
Originally Posted by newbie_01
Hi Don,

Thanks for your reply. I have to use your first suggestion, why didn't I think of that Smilie Reason being is that your second suggestion does not work on PDKsh and KSH88 Smilie ...I tried it out on the 3 servers that I need to run the script for and the second suggestion you mentioned only works for KSH93.

Good idea on using the return instead of exit. Just to clarify, the return will always return to the calling sub, correct?

BTW, I also just discovered that if I have more than 4 select options to choose, it wraps into the next column, is there any way to control this behavior?

Thanks again for reply.
Yes, ksh didn't add support for $'...' until ksh93.

I'm not sure what you mean by calling sub. In my example, menu_main was called from the main body of the ksh script. A return from a ksh function or dot script will return to the invoking shell script.

I'm also not sure what you mean by wraps into the next column. If you mean that adding more choices to the select makes that line in your script wider than your screen width or editing window width, you can use a
backslash newline to continue your choices onto the next line. For example:
Code:
#!/bin/ksh
menu_main1() {
        PS3="
::  Please choose a VALID option : "
        printf "\n\n"
        select choice_main in "Generic SA" "Refresh Tasks" "Others" \
                "Do Something Else" "Return"
        do
                case $choice_main in
                ( "Generic SA" )
                        echo "Generic SA"
                        clear
                        menu_db_admin;;
                ( "Refresh Tasks" )
                        echo "Refresh Tasks";;
                ( "Others" )
                        echo "Others";;
                ( "Do Something Else" )
                        echo Do Something else;;
                ( "Return" )
                        return;;
                esac
        done
}
echo about to call menu_main1
menu_main1
echo menu_main1 has returned

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to break the line to the one above?

Hello everyone! I'm trying to make the below file1 look like file2, can anyone help? Basically I just hit backspace on every line that starts with a number. Thanks! file1: THIS#IS-IT1 4 THIS#IS-IT2 3 THIS#IS-IT3 2 THIS#IS-IT4 1 Result > file2: (4 Replies)
Discussion started by: demmel
4 Replies

2. Shell Programming and Scripting

Is is possible to pass multiple entries in PS3 select ?

PS3="Enter corresponding number and hit enter:" select DIR in `cat mylist` QUIT do if then echo "INVALID INPUT" else if ; then my commands ..... else break fi fi REPLY='' done The above will return something like below : Select from the list of... (4 Replies)
Discussion started by: ningy
4 Replies

3. Shell Programming and Scripting

PS3 select when wrong input given

Hi All I am using the below code to chose a file to view : PS3="Select file to view : " select FILE in `ls` QUIT do if ; then clear cat $FILE else break fi REPLY='' done Everything works fine as long as I am giving the correct choice . But when i give a... (4 Replies)
Discussion started by: ningy
4 Replies

4. UNIX for Dummies Questions & Answers

VI Line Break?

So I'm in a Unix class and our assignment was to go into VI and write a script to make this file tree. At the end of it, I'd like it to echo "This is the file tree you've created" then a line break, then . But I'm not sure as to who to do it. Is there a way for when I run it (./filesystem), the... (4 Replies)
Discussion started by: bbowers
4 Replies

5. Shell Programming and Scripting

sed with line break

<td> CIS </td>and I tried to sed 's/<td>\/nCIS\/n<\/td>/<td><\/td>' and sed 's/<td>\/rCIS\/r<\/td>/<td><\/td>' , but no joy. This is an html page that I need to clean. (4 Replies)
Discussion started by: dba_frog
4 Replies

6. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

7. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

8. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

9. Shell Programming and Scripting

TO break a line

hi All, Have a doubt in ksh..Am not familiar with arrays but i have tried out a script.. plzzzzz correct me with the script My i/p File is: (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = 192.168.2.2) (Port = 1525) ) ) (CONNECT_DATA = (SID = TESTDB1) ) ) ... (7 Replies)
Discussion started by: aajan
7 Replies

10. Shell Programming and Scripting

Out put with select date. Help !!!

If I have a flatfile like vote.dat NAME | SEX | DATETIME | VOTE Jason|M|2005-12-10 08.01.30|Y Benson|M|2005-12-10 12.01.00|Y William|M|2005-12-10 08.01.09|Y Nick|M|2005-12-11 09.01.07|Y Pascal|M|2005-12-11 01.01.06|Y Mickey|F|2005-12-12 12.01.30|Y How can I write a korn script to have... (4 Replies)
Discussion started by: sabercats
4 Replies
Login or Register to Ask a Question