简体   繁体   中英

How do you redirect squirrel “print” command output to a file?

I have a Squirrel plugin as below:

fe.add_transition_callback( "removefavourite" );
function removefavourite( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.ChangedTag:
print( fe.game_info( Info.Name ) + "\n" );
system( "echo '" + fe.game_info( Info.Name ) + "' > /home/pi/.attract/romlists/ROMNAME.tmp" );
system( "printf '" + fe.game_info( Info.Emulator ) + "' > /home/pi/.attract/romlists/ROMNAME2.tmp" );
break;
}
return false;
}
fe.add_transition_callback( "removefavourite" )

I would like to redirect the output of the "print" command from terminal/console to a file. However, I cannot seem to do so and would be grateful if someone could assist me please.

I've tried alternatives in bash - many variations of the "echo" command and the "printf" command, but these are not effective to capture words which include parentheses eg () or single quotes eg '. The "print" command seems to be effective in all cases but I have not been able to redirect the output.

Please note that I am unable to modify the words I'm capturing first by escaping/backslashing special characters before sending them to print, echo or printf.

Thank you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM