0

I am using terminal emulator. I have a folder with save files in it and am trying to determine whether the entered text matches any file in the list.

I created a variable called saveFiles using the ls. Only displaying files with .save and removing it from the output:

saveFiles=$(cd "${0%/*}"/save; ls *.save* | ls *.save*; cd "${0%/*}")
echo -n ">"
read -r "name"  

So $saveFiles equals:

Savegame1 savegame2 savegame3

I'm trying to make an if statement that tests wether the entered variable equals any of the files in the folder.

The following script works except when I type a letter contained at the end of the file. So if one of the files is called savegame, if I type game it comes up with a match because game.save is contained in the string.

if [[ $saveFiles = *"$name".save* ]]
then
   scene=$(cat "save/$name".save)
fi

I need to find a way to test wether any of the strings in $saveFiles are equal to the entered variable $name.

To reiterate, files in folder:

Save1.save
Save2.save
...

Read `$name`

If $name matches any file in the list then load scene otherwise repeat.

I hope this isn't confusing. Please feel free to ask me to clarify further. Thank you.

jww
  • 97,681
  • 90
  • 411
  • 885
Psy
  • 3
  • 2
  • Welcome to the StackOverflow! You can format the programm code in your question better with the grave accent character ` ( for example `code`) or tripple ``` for multi-line code blocks – Tolbxela Aug 21 '19 at 08:53

1 Answers1

1

Maybe I am not understanding the question correctly, but why don't you first request the file name and then query the file system with precisely that name, e.g.

read name
if [[ -f "${name}.save" ]];
    echo "Found the file ${name}.save"
fi
dasmy
  • 569
  • 4
  • 10
  • The issue is when I type a save that is not in the list it sets the game to a non existent file name. I will see if this works. – Psy Aug 21 '19 at 08:58
  • This is exactly what I needed. Just to clarify. How does this query work. -f is find right. So it's asking whether a file in the folder is matches name? – Psy Aug 21 '19 at 09:02
  • Ok I see it's saying if the search matches the filename. I'm glad it was that simple. This worked beautifully. Thank you. – Psy Aug 21 '19 at 09:06
  • Great :-) For reference: I just fixed a typo - `read name` instead of `read $name`. – dasmy Aug 21 '19 at 09:23
  • Yeah I didn't see that. I had a further question. Is there a way to use the same -f query to test whether or not a file in the list contains .save? Trying to make a if statement returning "No saves in folder" – Psy Aug 21 '19 at 19:44
  • I know I could do it by setting my ls variable with saveGame = ls /save and then an if statement for if saveGame contains .save. but it seems like there is a shorter -f script for wether a folder contains files with .save in the name. – Psy Aug 21 '19 at 19:49
  • However when I use : saveFiles=$(cd "${0%/*}"/save; ls \*.save\*;) when the folder is empty I get this output: ls: \*.save\*: No such file or directory. I don't want it to say this. The variable should just be set to null. So looks like I still need an if [ -f ] script for if files contain .save else echo "no saves". – Psy Aug 21 '19 at 20:34
  • I think, the simplest approach is to check the return value of `ls *.save` as in https://stackoverflow.com/a/6364244/10984529. Note, how the output is redirected to make the statement silent. – dasmy Aug 22 '19 at 08:37
  • That worked perfectly: ```cd "${0%/*}"/save; if ls *.save 1> /dev/null 2>&1``` How exactly is this script working? I'd like to know what `1> /dev/null 2>&1` scecifically means broken down. – Psy Aug 22 '19 at 14:58