Bash question
\/ FORGET THIS, read my second post \/
Code:
while [ $continue = no ];do The third line is the only one that is really relevant. What is a simpler way of writing the third line? Basically instead of it checking for all those numbers, I just want to make sure that it is a number, and that it isn't more then 99. And for the other options, my Dad said he remembered that he used to do something like this: if [ "$answer" != [y|e|remove] ];then but he doesn't remember the exact syntax. And I tried that and it didn't work. So do any of you know the right way of doing that or something similar? |
But you're accepting "e", "y", and "remove", so your text explanation
doesn't match your code ... |
Ok forget about everything else. I figured out how to do the number part. I just want to know if there is something similar to this that is right:
if [ "$answer" != [y|e|remove] ];then This put in a sentence "If the answer is not y, e or remove then" My long current way of doing this that I want to shorten (because if I have like 20 options then this line could get really long): Code:
while [ $continue = no ];do |
This is maybe not very efficient, but it seems elegant:
Code:
correct_answer=0 jlinkels |
Quote:
I am still curious though if there is a right way of doing what I posted. I know my Dad used to do something like it and I have seen it a few places on the internet before, but when I wanted to use it I could never find it again. Anyway, thanks again jlinkels. |
A case statement is more natural and easily allows accepting Y as equivalent to y etc:
Code:
case $answer in |
Quote:
|
And a third version ... ;D
Code:
regex="y|e|remove" Cheers, Tink |
Quote:
|
Quote:
little scales on the far left of their posts, or click the "helpful" link within their posts ;} |
Quote:
One note though. A side effect of your solution is that you can enter a lot of other things that aren't in the variable "regex" and the answer will be accepted. I haven't quite been able to figure out the pattern yet but here are some examples: re reeeeeeeeeeeeee er yre |
Quote:
Code:
regex='^y$|^e$|^remove$' |
Quote:
|
Tink Tink Tink .... you must be getting slack in your old age ... lol .. just kidding:
Code:
regex='^(y|e|remove)$' Code:
case ${answer,,} in Code:
elements=(y e remove) |
Nice, grail, nice :) Robust and elegant
|
All times are GMT -5. The time now is 06:33 PM. |