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
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?
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)