0

#!ksh I wanted to do something weird, but unix is not letting me... Please look and the example:

#assign multiple values to a variable so if the user type 
#upper case or lower case it will be ok

var=aa1|AA1|aA1  

#then I want to use the variable in a guess script

I need to use the variable in other functions so I cannot use cases Can anyone help? Thank you.

2 Answers 2

2

The | character is a pipe, which has special meaning in KSH.

Wrap the entire string in single quotes and you'll be fine.

var='aa1|AA1|aA1'

Although if you're comparing with something like egrep later, you could just pass it '-i' and it will become case insensitive, or take the users's input and pass it through 'tr' to force it all upper or lowercase so you know what it will be.

Lots of easy ways to skin the cat.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work still, I am doing a guess scrip and I want to say if [ $variable2 != $var ], but as you wrote variable2 will only be equal var if I write aa1|AA1|aA1, but I only need to type one of those 3. :-). At least that is how my test is doing...
1

you might want to declare the variable as lower-case before you prompt the user for a value:

$ typeset -l x
$ read x
HELLO WORLD
$ echo "$x"
hello world

Another scenario to match what you're doing:

$ answer="hello world"
$ typeset -l response
$ read response
HELLO WORLD
$ if [ "$answer" = "$response" ]; then echo "Correct!"; fi
Correct!

3 Comments

Thanks for answering, but I still don't understand how I can use this in my script. Can I say typeset -l answer=Hello World? I am not getting the value of the variable from input, it meant to be set so I can use for testing my guess script.
I actually use the variable to compare if it is equal to the user input, but the variable is unchanged. I never used typeset before, that is why I don't get it. Sorry mate and thanks again.
Sorry for my dumpness, I understand typeset now and I manage to implement it on my script. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.