0

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.

Spud
  • 73
  • 5
  • Where are you invoking `bash`? `/bin/echo` is not `bash`, it is a separate program (`bash` has its own builtin called `echo`). – cdarke Jan 08 '19 at 14:11
  • Of course `>` is a shell metacharacter, but you do not appear to be running a shell anywhere. Did you try using `/bin/bash -c echo .....`? – cdarke Jan 08 '19 at 14:19
  • Thanks. I'm new to this. I have changed the question, removing the reference to bash. I have not tried the /bin/bash command. To be honest, I find the squirrel script extremely difficult to change at all without errors arising. Is there some other change to the code that I might be able to make whilst retaining the /bin/echo command? – Spud Jan 08 '19 at 22:04
  • 1
    (sorry for the delay, we are probably on different timezones). The problem is that you require the redirection functionality `>`, that is something that `bash` and other shells use, it is not a function of `echo`. All `echo` does it write to standard output stream. I know nothing about `squirrel`, so can't advise on that, but I really would try using `/bin/bash -c echo...` instead of `/bin/echo`. – cdarke Jan 09 '19 at 09:52
  • Thanks. It will be a timezone issue, definitely. I replaced the "/bin/echo" with "/bin/bash -c echo", but it still has a word expansion error. When I use "/bin/bash -c echo" from the commandline, it does create the REMOVEFAVOURITE.temp file, but without content. However, if I just use "echo" in its place, it will create the file with the content. – Spud Jan 09 '19 at 22:59
  • Would printf work instead? – Spud Jan 09 '19 at 23:23
  • `printf` would make no difference. – cdarke Jan 11 '19 at 10:46
  • All good, thanks. Figured it out below. Seems to work well at present. Thanks for taking your time to look at it for me. – Spud Jan 11 '19 at 11:36

1 Answers1

0

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".

Spud
  • 73
  • 5