LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2020, 04:24 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 1,020
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question Bash select string length for prompts?


Okay, I'm using the select syntax in my bash script to select an option.

The string length of the options seems to cause wrapping if longer than 11 characters, is there any way around this?

Here are two examples showing the effect

Option length 11 ch
Code:
#!/bin/bash
PS3='Please enter your choice: '
options=(
#123456789012#
"phone16kbps"
"sw   24kbps"
"mw-us40kbps"
"voice56kbps"
"Quit")
select opt in "${options[@]}"
do
    case $opt in
        "$1")
            echo "you chose choice $REPLY which is $opt"
            ;;
        "$2")
            echo "you chose choice $REPLY which is $opt"
            ;;
        "$3")
            echo "you chose choice $REPLY which is $opt"
            ;;        
        "$4")
            echo "you chose choice $REPLY which is $opt"
            ;;        
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done
Results in this:
Code:
charlie@charlie-machine:~$ options.sh
1) phone16kbps
2) sw   24kbps
3) mw-us40kbps
4) voice56kbps
5) Quit
Please enter your choice: 5
charlie@charlie-machine:~$
And adding one character, option length 12 ch:
Code:
#!/bin/bash
PS3='Please enter your choice: '
options=(
#123456789012#
"phone 16kbps"
"sw    24kbps"
"mw-us 40kbps"
"voice 56kbps"
"Quit")
select opt in "${options[@]}"
...
Results in this:
Code:
charlie@charlie-machine:~$ options.sh
1) phone 16kbps	 3) mw-us 40kbps  5) Quit
2) sw    24kbps	 4) voice 56kbps
Please enter your choice: 5
charlie@charlie-machine:~$
I think that illustrates the effect.

Maybe there's another way to select an option without using the select syntax, and I'm looking for a bash solution, I'd prefer not to use xenity or dialog.

Last edited by GPGAgent; 04-08-2020 at 04:41 AM.
 
Old 04-08-2020, 05:04 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
Select uses the COLUMNS variable to determine terminal width. Try adding the statement
COLUMNS=12
to the top of your script.
 
1 members found this post helpful.
Old 04-08-2020, 05:14 AM   #3
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 1,020

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by michaelk View Post
Select uses the COLUMNS variable to determine terminal width. Try adding the statement
COLUMNS=12
to the top of your script.
Brilliant - thanks - every day is a school day
Code:
#!/bin/bash
COLUMNS=12
PS3='Please enter your choice: '
options=(
#123456789012#
"phone 16kbps"
"sw    24kbps"
"mw-us 40kbps"
"voice 56kbps"
"Quit")
select opt in "${options[@]}"
...
Results in:
Code:
charlie@charlie-machine:~$ options.sh 
1) phone 16kbps
2) sw    24kbps
3) mw-us 40kbps
4) voice 56kbps
5) Quit
Please enter your choice: 5
charlie@charlie-machine:~$
So simple
 
Old 04-08-2020, 06:11 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Just for your inspiration. Once upon a time I wrote this script that selects from options. I ended up not using select, but there're several implementations in the source including two with select.
 
Old 04-08-2020, 09:19 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I realise the length issue was the original question, but you are aware that your case statement is not going to work, or at least definitely not like you think??
 
Old 04-09-2020, 05:13 AM   #6
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 1,020

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by grail View Post
I realise the length issue was the original question, but you are aware that your case statement is not going to work, or at least definitely not like you think??
You're correct, as written it doesn't work, but the length issue was attended to.

Here's one that works, or at least how I want it to:
Code:
#!/bin/bash
# Bash Menu Script Example

## A few aliases are also available for ABR mode:
## phone => 16kbps/mono        phon+/lw/mw-eu/sw => 24kbps/mono
## mw-us => 40kbps/mono        voice => 56kbps/mono
## fm/radio/tape => 112kbps    hifi => 160kbps
## cd => 192kbps               studio => 256kbps
COLUMNS=20
PS3='Please enter your choice: '
A="phone 16kbps/mono";B="sw    24kbps/mono";C="mw-us 40kbps/mono";D="voice 56kbps/mono";E="Quit"
options=("$A" "$B" "$C" "$D" "$E")
select opt in "${options[@]}"
do
    case $opt in
        "$A") break ;;
        "$B") break ;;
        "$C") break ;;        
        "$D") break ;;        
        "Quit") echo "quitting";exit ;;
             *) echo "invalid option $REPLY" ;;
       esac
done
echo "====================================="
echo "ABR preset is $opt - ${opt%% *}"
echo "Prompt for filename, title, artist etc"
echo "====================================="
echo "Now this will continue with the processing"
echo "part of the script......."
echo "====== Finished Okay ================"
exit
and a few terminal grabs to show it working:
Code:
charlie@charlie-machine:~/000TEST$ ./Test.sh 
1) phone 16kbps/mono
2) sw    24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice:                                       <===== blank entry
1) phone 16kbps/mono
2) sw    24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice: 6                                     <===== invalid selection
invalid option 6
Please enter your choice: 4                                     <===== valid selection
=====================================
ABR preset is voice 56kbps/mono - voice
Prompt for filename, title, artist etc
=====================================                            <===== processing part with option 4 
Now this will continue with the processing
part of the script.......
====== Finished Okay ================
charlie@charlie-machine:~/000TEST$ ./Test.sh 
1) phone 16kbps/mono
2) sw    24kbps/mono
3) mw-us 40kbps/mono
4) voice 56kbps/mono
5) Quit
Please enter your choice: 5                                       <===== quit option
quitting
charlie@charlie-machine:~/000TEST$
Job done, thanks everyone
 
Old 04-10-2020, 02:20 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Cool, here is what I like to do, if you like some of it steal it, if not it is here for future onlookers
Code:
#!/usr/bin/env bash

COLUMNS=20
PS3="Please enter your choice: "

options=(
				 "phone 16kbps/mono"
				 "sw    24kbps/mono"
				 "mw-us 40kbps/mono"
				 "voice 56kbps/mono"
        )

# Select from array plus adding Quit option so array can be added to easily
select opt in "${options[@]}" Quit
do
                # Catching the issue where the user inputs non-numerical entries
		[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))

                # Break on any valid choice as information stored in opt is what is required
		if (( REPLY <= ${#options[*]} + 1 ))
		then
			break
		else
				echo "ERROR: Invalid choice.  Must choose one of the numbers provided."
		fi
done

if [[ "$opt" == Quit ]]
then
	echo "quitting"
else
        # Use heredoc to output large amounts of text
        # Hyphen (-) before 'OUT' is impoartant as it allows for formatting in editor to be constant
        # and all tabs prior to text in heredoc will be removed
	cat <<-OUT
		=====================================
		ABR preset is $opt - ${opt%% *}
		Prompt for filename, title, artist etc
		=====================================
		Now this will continue with the processing
		part of the script.......
		====== Finished Okay ================
	OUT
fi
Sorry for the weird indenting, my tabs are only 2 spaces in size, but bigger here

Last edited by grail; 04-10-2020 at 10:51 PM. Reason: Comments added
 
1 members found this post helpful.
Old 04-10-2020, 12:37 PM   #8
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 1,020

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by grail View Post
Cool, here is what I like to do, if you like some of it steal it, if not it is here for future onlookers
Code:
#!/usr/bin/env bash

COLUMNS=20
PS3="Please enter your choice: "

options=(
				 "phone 16kbps/mono"
				 "sw    24kbps/mono"
				 "mw-us 40kbps/mono"
				 "voice 56kbps/mono"
        )

select opt in "${options[@]}" Quit
do
		[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))

		if (( REPLY <= ${#options[*]} + 1 ))
		then
			break
		else
				echo "ERROR: Invalid choice.  Must choose one of the numbers provided."
		fi
done

if [[ "$opt" == Quit ]]
then
	echo "quitting"
else
	cat <<-OUT
		=====================================
		ABR preset is $opt - ${opt%% *}
		Prompt for filename, title, artist etc
		=====================================
		Now this will continue with the processing
		part of the script.......
		====== Finished Okay ================
	OUT
fi
Sorry for the weird indenting, my tabs are only 2 spaces in size, but bigger here
Grail, very nice, very neat, I'm going through it line by line, but for others it may be useful to comment on each line what it's doing.

Cheers
 
  


Reply

Tags
bash script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace a string of blanks with a "tailored" string of equal length danielbmartin Programming 6 10-16-2015 11:07 AM
[SOLVED] Convert length-indicated variable length record file to LF-terminated Z038 Linux - General 10 11-29-2012 11:59 PM
Please help fix: sky2 error: rx length error: status 0x42c0500 length 600 trapix22 Linux - Wireless Networking 1 10-25-2008 07:50 AM
(Samba 3.0.20) Vista prompts for username, XP prompts just for password Noffie Linux - Server 2 07-21-2008 10:26 AM
Get string length in C ++ for a c string without trailing \0 nc3b Programming 10 12-28-2007 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration