how to test input variable is a string in a select loop

Okay -- I hope I ask this correctly.

I'm working on my little shell script to write vendor names and aliases to files from user input. If a user choose to add to a file, he can do that as well. I'm using a select loop for this function to list all the possible files the user can choose from. Here is the code I have so far

echo
        PS3='FILE YOU WANT TO MODIFY: '
	options
        echo
	echo ADD DATA TO FILE:  FILES IN THE VENDORLIST FOLDER
	echo 
		select file in $(ls ~/vendorlists)  #uses file as the variable for the chosen file and lists all the files in the VENDORLIST folder
        		do
			echo							
                           open ~/vendorlists/$file
                         break
                         done

The user can enter P to go back to the previous menu, E to exit the script entirely, or M to go back to the main menu.

the open statement is temporary just so I could see if the function would even open the file. My problem is that when I tried using a case statement inside the select loop to test if the user entered P, E or M, then nothing happened. I know the select statement puts the files in a list and asks for which one the user wants. But how do I test to see if the user's choice is a P, E or M and take the appropriate action, or is in not possible to do that with the select statement? My guess is that with the select statement the variable is an integer and not a string. So can I test to see if the variable is an string or not, and if so, how?

Well, unless you typeset they are all strings, but you can regex or file glob test them with various test, like case, or substitute operators in shell. For instance, "${X#[a-zA-Z]" says remove the first character if it is a letter, after which that expression is not equal to $X, if it was a letter. The file glob wild cards of case and ${NAME#%EXPR} are not big of length specifiers, so a regex construct like grep works better. Are your numbers all positive (no -), all integer (no .)? An extended regex of '^[0-9]+$' says a line of one or more digits (and nothing else).

I don't follow. What I meant to say was when I use the select command, it makes a list of all the files in the folder, each file being preceeded by a number. In order to choose the file, I simply enter the number next to it.

That's not the problem. What I encountered was the option of choosing a P, E or M to go to the previous menu, or to exit the script entirely, or to go back to the main menu -- those are choices that the select command doesn't list, only the files in the folder from which I have to choose. So what I wanted to know was does unix interpret the choice made from the list as a number or a string? And how do I get the function to test for the possibility of the user entering a P, E or M and taking the appropriate action?

The shell variable REPLY contains what the user selected from the menu eg 1,2,E,Q or M.

The variable file contains the filename they selected or blank if they type a number or letter not on the menu.

So you can do

select file in ~/vendorlists/*
do
    case $REPLY in
         E|M|Q) ACTION=$REPLY; break ;;
    esac
    if [ -z "$file" ]
    then
         echo "Invalid choice"
    else
         open ~/vendorlists/$file
         break
    fi
done

Thanks Chubler_XL!

I have another question, if I may -- I apologize if it's an obvious answer -- still learning. Why two different variables in the loop, $file and REPLY, just to make this perfectly clear.

And by the way, it doesn't echo invalid choice if I input a non vaild answer -- it just breaks out of the script and returns me to the prompt.

Um --- how do I get benkids123's post removed? I don't need to deal with this.:mad:

The variable REPLY is set by the select command and is what the user typed to the prompt. If REPLY is a valid selection select will assign your variable (file) to the value that equates to the user typed selection.

So for select fruit in apple pear peach if user types 1 you get REPLY=1 fruit="apple" if user types Q you get REPLY=Q fruit="" so you can see fruit will always be one of the valid choices or blank where REPLY is just what the user typed.

The code I posted works fine for me in both bash and ksh, please check you have copied the code as shown and if your sure it's fine can you supply some info on the shell and OS you are using.

And shell variables hold strings, unless typeset (and not exported in -- the exported/inherited env is all strings) . . . .