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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to test input variable is a string in a select loop
# 1  
Old 01-24-2011
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

Code:
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?
# 2  
Old 01-24-2011
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).
# 3  
Old 01-24-2011
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?
# 4  
Old 01-24-2011
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

Code:
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

# 5  
Old 01-24-2011
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.
# 6  
Old 01-24-2011
Um --- how do I get benkids123's post removed? I don't need to deal with this.Smilie
# 7  
Old 01-25-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

2. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

3. Shell Programming and Scripting

hlp.Can I Input string from variable?

My Os is Solaris 10, i want to create user after created i use #passwd user1 new password : confirm password : so i typing password into input box by manual but i need bring username input into input box by automatic can somebody help me plz. if you misunderstand or help me can you... (0 Replies)
Discussion started by: infjustice
0 Replies

4. Shell Programming and Scripting

append to same string variable in loop

I want to append values to same string variable inside a recursive function that I have .. I do not want to write to any file but use a variable.. Can anyone please help with it? Thanks in advance. (6 Replies)
Discussion started by: Prev
6 Replies

5. Solaris

how i can input string from variable

hello every body my system is solaris 8 and my problem is i have a lot of user to change password so i'm key password every user and i want to input password from variable my script grep xxx shadow |while read a do passwd $a done #new password... (2 Replies)
Discussion started by: infjustice
2 Replies

6. Shell Programming and Scripting

Adding string variable dynamically in for loop

Hi, I need to generate the text name dynamically in for loop, ex, VAR_COPY_FILE1= file path 1 VAR_COPY_FILE2= file path 2 VAR_COPY_FILE3= file path 3 for i in 1 2 3 do if then "do some process here" fi done (3 Replies)
Discussion started by: msubash26
3 Replies

7. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

8. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

9. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

10. Shell Programming and Scripting

How to select only the most frequent instances of a variable string in a file?

I've got a web access file that I want to grep (or awk or perl or whatever will work!) out the most frequent instances of unique IP entries. Meaning the file looks something like this: I'd like to run a sort or grep (or whatever) that will only select out the lines from IP's that had the... (7 Replies)
Discussion started by: kevinmccallum
7 Replies
Login or Register to Ask a Question

Featured Tech Videos