Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Bash: Syntax questions about this script's errors

null_reflections

Legendary Coder
Code:
read greeting

case "$greeting" in
    "fu*k you" ) echo "fu*k you too!"
    ;;
    [Nn][Yy] ) echo "I think you meant to type yes or no..."
    ;;
    "Hello?" ) echo "Hello there, aren't you sexy!"
    ;;
    "i hate you" | "die, please" | "nerd" | "fu*k off" )
    echo "wow! soooo polite!"
    ;;
    * ) echo "try again!"
    continue
    ;;
esac

So above we have a functioning (yet very rude) case/switch script, where a user inputs any one of those responses on the left before the ")", and the shell answers with what matches after "echo". It took me over 20 minutes to make this because of a couple of different things:

Code:
[Nn] [Yy]

Why does bash give an error if there is white space between them?

Code:
 "i hate you" || "die, please" || "nerd" || "fu*k off"

Why do these need a pipe instead of an or symbol?

Again, this is just an exercise, i try to make them funny sometimes since no one else would care/understand anyway ;-)
 
Code:
read greeting

case "$greeting" in
    "fu*k you" ) echo "fu*k you too!"
    ;;
    [Nn][Yy] ) echo "I think you meant to type yes or no..."
    ;;
    "Hello?" ) echo "Hello there, aren't you sexy!"
    ;;
    "i hate you" | "die, please" | "nerd" | "fu*k off" )
    echo "wow! soooo polite!"
    ;;
    * ) echo "try again!"
    continue
    ;;
esac

So above we have a functioning (yet very rude) case/switch script, where a user inputs any one of those responses on the left before the ")", and the shell answers with what matches after "echo". It took me over 20 minutes to make this because of a couple of different things:

Code:
[Nn] [Yy]

Why does bash give an error if there is white space between them?

Code:
 "i hate you" || "die, please" || "nerd" || "fu*k off"

Why do these need a pipe instead of an or symbol?

Again, this is just an exercise, i try to make them funny sometimes since no one else would care/understand anyway ;-)

Might want to look up the OR operator for bash 😉 HINT HINT WINK WINK
single pipe means you are passing the result of the current command to the next command
double pipe means OR
 
To address the first question:

Why does bash give an error if there is white space between them?
I'm not a big bash expert but I think it's because [Nn] [Yy] is a string and should therefore be surrounded by quotes just like the other strings in the case. If [Nn][Yy] works, it would seem that bash only requires the quotes when the string contains whitespace. Personally I would use the quotes anyway (unless that breaks it of course).
 
Online it says 2 operands, I'm wondering if you could do something like...x OR y OR z, I would be more descriptive but I'm away on my phone
here's a little something for ya 😉
To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom