1

I'm having a bit of trouble getting this to work/ knowing if its possible. I'm creating a game using little other than bash, this requires a lot of repeated case statements. I am trying to load all the repeated case statements into a variable, then repeat them when necessary to limit the amount of work it will take to update the shared case statements between different scripts. Here is what I have:

#!/bin/bash

moo="[m][o][o]) echo 'thank you for following instructions' ;;"

test=$(echo "while true ; do
read -p 'type moo: ' case
case $case in
$moo
*) echo 'type moo please'
esac
done")

"$test"

The problem I run into is:

./case.sh: line 13: $'while true ; do\nread -p \'type moo: \' case\ncase  in\n[m][o][o]) echo \'thank you for following instructions\' ;;\n*) echo \'type moo please\' ;;\nesac\ndone': command not found

The information in the moo variable will eventually be in a separate script and will be set by invoking it as a function within that script when I finally get a working model. It looks like this is a workable idea, I've just reached a loss on how to invoke the variable without it acting up. If anyone has any ideas, I would greatly appreciate it. Thank you in advance!

3
  • It looks like you're trying to store code as strings in variables. Why not just use functions? Commented Sep 8, 2018 at 17:27
  • The goal is to be able to store the ~20 repeated case statements in a central location for use by the 68 different scripts that will use them. there are also be parts of the case statement that is specific to the individual. script The best I could come up with to that end has been loading them into a variable then echoing the statement into a variable, to load the repeated statements, then running that variable to create the loop. I have tried some functions to that avail, but have had no success. any suggestions welcome. Commented Sep 8, 2018 at 17:40
  • 2
    Without an explanation of what you actually hope to achieve and why functions haven't worked for you so far, it's hard to know what to suggest. Post a new question with that information instead maybe? Commented Sep 8, 2018 at 17:54

1 Answer 1

1

It doesn't work because the quotes make the variable expansion be treated as a single word.

But it wouldn't work without quotes, either, because the shell doesn't parse the output of variables for syntax like semicolon and newline. Variable expansion is done after that stage of command parsing. The only processing that's done on expanded variables is word-splitting and wildcard matching.

You need to use eval to perform all command parsing:

eval "$test"

Another problem is that the variable $case is being expanded when you assign the variable test, it's not getting the value being read by read. Since the variable doesn't have a value yet, it's being executed as:

case in ...

and this is invalid syntax. You need to escape the $ so it will be passed through literally.

There's also no need for echo, you can simply assign the string directly.

test="while true ; do
read -p 'type moo: ' case
case \$case in
$moo
*) echo 'type moo please'
esac
done"
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, however now it is getting hung up on : ./case.sh: eval: line 17: syntax error near unexpected token [m][o][o]' ./case.sh: eval: line 17: [m][o][o]) echo 'thank you for following instructions' ;;'
Getting case to work inside eval is extremely tricky. I suggest you read mywiki.wooledge.org/BashFAQ/050 and abandon this approach.
You need to escape the $ in $case
thank you for all your help! looks like its working!

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.