Squirrel script - whitespace problem - add a "space" to pass to "echo" command

145 Views Asked by At

I have a squirrel plugin which invokes "echo". It is almost correct, but despite having considered various responses on stackoverflow to this problem (and on other unix-related sites) as to how to deal with the "whitespace" or metacharacter issue, I have not been able to get "echo" to work.

The squirrel plugin is as follows:

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 ) + "\"" + " > " + "\"" + "/home/pi/.attract/romlists/REMOVEFAVOURITE.temp\"");
    return false;
}}

The error I receive is as follows:

The parameter word expansion failed. ["Sam's Journey (Easyflash)" > "/home/pi/attract/romlists/REMOVEFAVOURITE.temp"].

The code is effective to pass the output to the terminal. However, it will not redirect the output to the REMOVEFAVOURITE.temp file. The problem appears to be the whitespace surrounding the " > " or the ">" itself.

I've tried dozens of alternatives to the " > ", but none has worked. How do I create a "space" to pass to the script which is acceptable to it please? Thanks.

1

There are 1 best solutions below

0
Spud On

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:

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:

#!/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".