Writing a text adventure game in BASH

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
UOOP

Writing a text adventure game in BASH

Post by UOOP »

So I am working on a text adventure game which I am writing in BASH. I am having some issues with trying to code my shop:

I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ? I want to put this within a while loop, something like:

while read -p "-->" var; do
if [ "$var" = "buy item" ]; then
if (add command to verify file is present in directory); then
echo "item purchased"
else
echo "item not available"
fi
fi
done

I have tried a few different commands and I will either end up getting item purchased every time, or item not available every time. Any ideas?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
fabien85
Level 7
Level 7
Posts: 1877
Joined: Tue Mar 11, 2014 4:30 pm

Re: Writing a text adventure game in BASH

Post by fabien85 »

What you want is [ -f ]. Example :

Code: Select all

$ touch item
$ [ -f item ] && echo "item purchased" || echo "item not available"
item purchased
$ rm item
$ [ -f item ] && echo "item purchased" || echo "item not available"
item not available
Good luck with your game ! :D
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Writing a text adventure game in BASH

Post by Termy »

You should also most definitely, absolutely, undeniably use at the very least some standard indentation. :P I wouldn't be happy until it looked like this, regarding fairly standard indentation:

Code: Select all

while read -p "-->" var; do
	if [ "$var" = "buy item" ]; then
		if (add command to verify file is present in directory); then
			echo "item purchased"
		else
			echo "item not available"
		fi
	fi
done
By the way, you may wish to add a space after the read prompt; makes things look less squished.
I'm also Terminalforlife on GitHub.
MintBean

Re: Writing a text adventure game in BASH

Post by MintBean »

Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.
whm1974
Level 4
Level 4
Posts: 241
Joined: Fri Jan 19, 2018 11:07 pm

Re: Writing a text adventure game in BASH

Post by whm1974 »

MintBean wrote:Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.
There are many FOSS Infocom style text game engines available. I would use one of those.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Writing a text adventure game in BASH

Post by Termy »

MintBean wrote:Only do this if your aim is to become a master of Bash, otherwise pick any of a number of programming languages and have a much more pleasant experience.
I have a very, very pleasant experience writing shell programs. Not sure why people hate on it so much, as it's much more flexible than people realise, and I'd argue very approachable. Shell is also ideal for embedded systems and as the glue which ties together other programs, not to say that shell (specifically, bash) isn't very capable on its own. I've written and still maintain many shell programs, and related tools (such as function libraries and bash completion scripts), some extensive and complex, some small and simple, and each one I find enjoyable to write and maintain. I'm not saying it's perfect, but it's another tool in the toolbox.

Point? How you feel doesn't dictate how everyone else feels. To each their own, basically.
I'm also Terminalforlife on GitHub.
lmuserx4849

Re: Writing a text adventure game in BASH

Post by lmuserx4849 »

UOOP wrote: I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ?
I believe what you need is the test command, which can be implemented via [ or [[. To see doc, type help test or man bash and look for the section CONDITIONAL EXPRESSIONS

if [[ ! -e "${filename}" ]]; then echo "File does not exist: ${filename}"; else : ; fi

declare -- filename=''; while [[ ! -e "${filename}" ]]; do read -r -p 'Enter some data: ' filename; done

echo can be replaced with printf -- 'Files does not exist: %s\n' "${filename}"

Just for fun - caterpillar game written in bash :-)
Last edited by lmuserx4849 on Wed Jan 31, 2018 12:57 pm, edited 1 time in total.
UOOP

Re: Writing a text adventure game in BASH

Post by UOOP »

lmuserx4849 wrote:
UOOP wrote: I know how to move an "item" from one location to another with mv, but what I cannot figure out is how to echo "Item not available" if the player were to try and purchase the said "item" again. which command could I use to check for the item and if it's not held within the chosen directory or file, echo "Item not available" ?
I believe what you need is the test command, which is can be implemented via [ or [[. To see doc, type help test or man bash and look for the section CONDITIONAL EXPRESSIONS

if [[ ! -e "${filename}" ]]; then echo "File does not exist: ${filename}"; else : ; fi

declare -- filename=''; while [[ ! -e "${filename}" ]]; do read -r -p 'Enter some data: ' filename; done

echo can be replaced with printf -- 'Files does not exist: %s\n' "${filename}"

Just for fun - caterpillar game written in bash :-)

Thanks for the feedback, lmuserx4849! Code worked perfectly :D
UOOP

Re: Writing a text adventure game in BASH

Post by UOOP »

Termy wrote:You should also most definitely, absolutely, undeniably use at the very least some standard indentation. :P I wouldn't be happy until it looked like this, regarding fairly standard indentation:

Code: Select all

while read -p "-->" var; do
	if [ "$var" = "buy item" ]; then
		if (add command to verify file is present in directory); then
			echo "item purchased"
		else
			echo "item not available"
		fi
	fi
done
By the way, you may wish to add a space after the read prompt; makes things look less squished.
I don't know what happened, but I did have the code indented :D Probably not exactly like that, but it was.

When I switched to Linux, I started learning terminal commands and then found out you could put those commands within a script very easily. I tried a couple of simple scripts and got hooked. I'm the kind of guy that likes to work from the ground up, knowing what's behind the scenes. This is why I didn't bother trying to use anything else. Others have asked "why don't you use a generator?" Well that's no fun is it? I guess it's an "I built this by hand" type of thing. It takes a while to write something out if you don't create short cut notes (like I do :D). So far I have enjoyed it and I love the challenge.
Locked

Return to “Scripts & Bash”