Author Topic: Echo command and fe.game_info not working with roms which have ', or () or []  (Read 4694 times)

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
I have a module/plugin which generally works.  It's designed to take the name of the rom and dump it into a text file for further processing.  It works with most games but I can't get it to work with rom names that include a single apostrophe (eg Sam's Journey) or games with any kind of brackets (eg Sam's Journey (Easyflash) or Sam's Journey [Easyflash]).  I was hoping someone might be able to help please.

The module is as follows:

Code: [Select]
fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ChangedTag:
                system( "echo '" + fe.game_info( Info.Name ) + "' > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp" ); // Takes the romname "unfavourited" by me in Favourites.txt and inserts it into REMOVEFAVOURITE.temp for further processing
        return false;

Interestingly, I can get the code to work from the terminal eg
Code: [Select]
echo "Sam's Journey (Easyflash)" > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp and
Code: [Select]
echo "Sam's Journey [Easyflash]" > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp, but not if single quotations are used eg
Code: [Select]
echo '"Sam's Journey (Easyflash)"' > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp
I don't think it's a problem with the fe.game-info part of the code.  It probably has something to do with the multiplicity of single and double quotations.  The use of the single quotations eg
Code: [Select]
echo '" + fe.game_info( Info.Name ) + "' in this code seems to mean it won't recognise the brackets, but it appears to need the single quotations in the module/plugin for it to work at all with other games that don't have brackets or apostrophes in their names. 

I've also tried printf instead of echo with the same result.

If anyone can help please, I would be grateful.  Thanks.


zpaolo11x

  • Hero Member
  • *****
  • Posts: 1233
    • View Profile
    • My deviantart page
Why don't you use the "print" command from AM and redirect the console output to a desired file? It should be more robust than "echo"

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Sounds like a great idea.  Could I please ask how I use this command?  Thanks.

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
I have tried this (and various variations) but haven't got it to work as yet:

Code: [Select]
fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ChangedTag:
                local romname = fe.game_info( Info.Name );
print( + romname + ) > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp;
// system( "echo '" + fe.game_info( Info.Name ) + "' > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp" ); // Takes the romname "unfavourited" by me in Favourites.txt and inserts it into REMOVEFAVOURITE.temp for further processing
system( "echo '" + fe.game_info( Info.Name ) + "' > /home/pi/.attract/romlists/ROMNAME.tmp" ); // Takes the romname "unfavourited" by me in Favourites.txt and inserts it into ROMNAME.tmp for further processing
system( "echo '" + fe.game_info( Info.Emulator ) + "' > /home/pi/.attract/romlists/EMULATORNAME.tmp" ); // Takes the emulator name "unfavourited" by me in Favourites.txt and inserts it into EMULATORNAME.tmp for further processing
// system( "sudo /bin/bash /opt/retropie/configs/all/removefavourite.sh" ); // Starts the process of removing the game from Favourites.txt
break;
}
        return false;
}

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
If you want to make sure a favorite was unfavorited, you should probably test var within your transition for Info.Favourite, and then see if it’s currently a favorite. If not, you know it’s a game that has been removed from favorites.

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Thanks, keilmillerjr and zpaolo11x.  I have manually checked both the favorites.tag and the c64.tag (where Sam's Journey should appear) and Sam's Journey remains listed in both of these. 

But I have made some progress using Attract Mode's internal print command, rather than echo or printf, as follows:

Code: [Select]
// This module is just to be used for removing favourites from Favorites.txt
fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.ChangedTag:
print( fe.game_info( Info.Name ) + "\n" );
break;
}
return false;
}
fe.add_transition_callback( "removefavourite" )

I now need to be able to redirect the output from the console to a file.  I've tried replacing the print line with the following:

Code: [Select]
print( fe.game_info( Info.Name ) + "\n" ) > /home/pi/.attract/romlists/REMOVEFAVOURITE.temp;
But wind up with an error in console.  I get a "line 8 column 46" "expression expected" error.  It's clearly something to do with the redirection to the file.  Is there a way to redirect output from the console to a file?  Do I need to use quotation marks or something like that?  Thanks.

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1233
    • View Profile
    • My deviantart page
I now need to be able to redirect the output from the console to a file.  I've tried replacing the print line with the following:

I actually misread your first post (being on my phone), I see now that you need multiple output files, which is something I don't think you can do using "print" command...

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
That's no problem.  I guessed as much after looking at the Squirrel 3.1 documentation.  I tend to worry at these problems until I've defeated them.

I've now started having a look carefully at fe.plugin_command and have made a considerable amount of progress with only a "whitespace" problem now defeating me.

I've had a good look at this thread:  http://forum.attractmode.org/index.php?topic=1705.msg12673#msg12673 and adapted some of the code.  My code now looks like this:

Code: [Select]
fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.ChangedTag:
fe.plugin_command( "/bin/echo", "\"" + fe.game_info( Info.Name ) + "\"" + "1>/home/pi/.attract/romlists/REMOVEFAVOURITE.temp\"");
        return false;
}}

This code almost works.  It prints  "Sam's Journey (Easyflash)" to the console.  However, there is an error in trying to redirect that code to the REMOVEFAVOURITE.temp file.  It needs a "space" before the "1>/home/pi/.attract/romlists/REMOVEFAVOURITE.temp".  However, everything I've tried including just adding a space before the " 1>" (like so) has failed.  It is an identifiable problem with bash commands, but I can't figure out how to deal with it in this case given that it is part of a squirrel script.

The error is: "Parameter word expansion failed".

Would you know how to add a "space" that bash might recognise?

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
I managed to solve this problem I was having. I found "keeping it simple, stupid" and just having the plugin deal with one argument was the way to go.

I altered the plugin as follows:
Code: [Select]
// This module is just to be used for removing favourites from Favorites.txt
fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.ChangedTag:
fe.plugin_command( "/usr/bin/printf1.sh", "\"" + fe.game_info(Info.Name) + "\"" );
system( "sudo /bin/bash /opt/retropie/configs/all/removefavourite.sh" ); // Starts the process of removing the game from Favourites.txt
}
return false;
}
fe.add_transition_callback( "removefavourite" )

This plugin sends the game's name to a new bash script I created called "printf1.sh".  The bash script goes in the same folder as the "printf" command which is in the "/usr/bin/" folder.  The bash script has to go into this folder, otherwise there is a "chdir" (change directory) error.

The contents of the bash script are:
Code: [Select]
#!/bin/bash
FILE1=$1
sudo /usr/bin/printf "$FILE1" > "/home/pi/.attract/romlists/REMOVEFAVOURITE.temp"

Basically, the game name is the "argument" and that is sent from the squirrel plugin to the bash script which then redirects the game name (the output) to the file "REMOVEFAVOURITE.temp".

The benefit of this approach is that no matter what form the game name takes eg with a single apostrophe or () or [] like "Sam's Journey (c64)" or "Sam's Journey [c64]", the script will capture it and pass it on.  Special characters make no difference.

From there, I can do whatever I like with the information recorded in "REMOVEFAVOURITE.temp".