1

So I'm trying to make a dungeon and I want to have 2 different responses when I enter the one room but I thats as far as I can get. How do I get 2 different outcomes from 1 file?

So when you open a file for the first time I want it to display a specific content, when you return to that file and reopen it I want it to display a different content. However, both contents are in the same file. Just want one content to read for the first visit and the second content to read upon repeat visits.

Is it possible to have two separate outputs to one file. Example, if you run the file below for the first time you get the response of "sup sup" and if you were to run it a second time you would get the "no no" response. This is just an example but thats what I'm trying to get at. So how exactly can I manage that?

this=1
if [ "$this"=="1" ]; then
echo "sup sup"
let "this=$this - 1"
fi
if [ "$this"=="0" ]; then
echo "no no"
fi
slm
  • 369,824
Ash Ly
  • 11
  • Is the number of visits/opening the file per user basis? That is, if user xyz opens the file for the 1st time, he sees 'This content'. When he opens it for the 2nd,3rd,4th...nth time, he sees 'That content'. Now, if user abc opens the file for the 1st time(but after xyz has opened the file already), you want the user 'abc' to see 'This content' or 'That content' ? – Sreeraj Nov 26 '14 at 17:44
  • Yes opening the file per user basis. So I want the user to see 'this content' and when the same user retuns and opens it, they'll see 'that content.' – Ash Ly Nov 26 '14 at 18:21
  • You could use a hidden file that is created by the script in user's $HOME when you run it for the first time, e.g. in one line : [ -f /$HOME/.dungeonscriptwasrun ] && echo "no no" || echo "sup sup"; touch /$HOME/.dungeonscriptwasrun. Note however that this will only work as intended if you remove that file when the application is closed (so that on subsequent runs, your script starts fresh). – don_crissti Nov 26 '14 at 18:55
  • So here is what I have, I'm running a Dungeon.sh and in the Dungeon.sh it would be something like this

    GAME=Start while [ "$GAME"=="Start" ] do sh dungeon1 (inside this file I will have two outputs I want to be displayed such as: when you open the file for the first time you would get the content "Sup, Sup/ Would you like to continue", and then the second content would be "You're back where you started". So I want the "You're back where you started" when you return to dungeon1. so continuing on in the dungeon.sh) read RESPONSE case $RESPONSE in "Continue") sh dungeon2;; "Return") sh dungeon1;

    – Ash Ly Nov 26 '14 at 19:11
  • @AshLy - add the comments to your post and then remove the comments, that way it's easier for people to understand what you want. – don_crissti Nov 26 '14 at 19:23
  • You need spaces around the equal signs: if [ "$this" == "1" ] (or rather if [ "$this" -eq 1 ] if you want a numeric comparison rather than a string comparison). – Gilles 'SO- stop being evil' Nov 26 '14 at 23:34

2 Answers2

1

You can use the $RANDOM variable from within Bash scripts to get something different each time you call it. It returns integers (0-32767) but you could test to see if the numbers are even or odd and then make that into a yes or no.

$ echo $RANDOM
2104

$ echo $RANDOM
25188

Even/Odd Example

$ if (( RANDOM % 2 )); then echo even; else echo odd; fi
even
$ if (( RANDOM % 2 )); then echo even; else echo odd; fi
even
$ if (( RANDOM % 2 )); then echo even; else echo odd; fi
odd

The above is using the modulo division operator to divide the currently $RANDOM number by 2. If $RANDOM is even, then we echo out the message, "even", otherwise we echo out "odd".

This can be adapted into a "yes" or "no" option that you could use within your script.

References

slm
  • 369,824
  • I also don't want to use random variables because I want a specific content. I want the first content to play out only once per user and the second content to repeat itself for the remainder of the program. Is that possible? – Ash Ly Nov 26 '14 at 18:33
  • @AshLy - you want it to toggle back and forth or act more like you accessed the file the 1st time you get X, thereafter you get Y? BTW, your question needs to be more specific, it's too vague still. Try and capture exactly it is you want. Be as specific as you can! – slm Nov 26 '14 at 18:35
0

That will depend on how the content is arranged in that file, however you'll just have to keep track of whether you've opened that file in the script.

# Example where the first time opening, line 1 is printed, and subsequent times
# line 2 is printed
file_opened=0
# ... code
if [ $file_opened -eq 0 ]; then
    # code for first time opening, e.g.:
    head -1 the_file.txt # Prints line 1
    file_opened=1
else
    # code for subsequent times, e.g.:
    sed -n '2{p;q}' the_file.txt # Prints line 2
 fi
outlyer
  • 1,103