shell scripting topic
-
@hiulit Another tip for your fun-facts-script project:
Declare a variable for
fun_facts.txt
, set the file's full path to the variable, and use the variable when you need to reference that file.It avoids some
No such file or directory
errors when you invoke the script from another directory.Example:
~/src/funfacts $ ls es-fun-facts-splashscreens.sh* fun_facts.txt ~/src/funfacts $ ./es-fun-facts-splashscreens.sh Mario first appeared in 1981 as the playable character in Nintendo's arcade game Donkey Kong. However, he was a carpenter known only as Jumpman ~/src/funfacts $ # THE SCRIPT RUNS FINE ~/src/funfacts $ cd .. ~/src $ ./funfacts/es-fun-facts-splashscreens.sh shuf: fun_facts.txt: No such file or directory
EDIT
This is the best method I've found to detect the script's dir:
readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if your
fun_facts.txt
is in the same file as the script, then you can just add the following line right after theSCRIPT_DIR
definition:readonly FUN_FACTS_TXT="$SCRIPT_DIR/fun_facts.txt"
-
@meleu Thank you! Done! Works perfectly :D
Could you elaborate on how$(cd "$(dirname "$0")
works? What does$0
mean? Thanks again! -
I found that if I specify a font when using
convert
it decreases the image creation time, like a lot!
It also depends on the sentence's length. But when specifying a font I get pretty good results with a 3 line sentence :Oreal 0m3.338s user 0m2.472s sys 0m0.824s
compared to:
real 1m3.849s user 0m12.148s sys 0m1.024s
-
@hiulit said in shell scripting topic:
Could you elaborate on how
$(cd "$(dirname "$0")
works? What does$0
mean?$0
is a positional parameter where the string you used to call the script is stored. More info about positional parameters can be found here (by the way, this doc has valuable info about bash scripting).Now let me elaborate why I think that one-liner to get the script dir is a good way to go...
To ilustrate let's consider that the script was invoked from a different directory than the one where the script is placed. Let's consider the scenario I used as example on my post above, where I am calling the your script with this line:
prompt$ ./funfacts/es-fun-facts-splashscreens.sh
Now I'm going to break down this variable definition line (skipping the parts that I think you already know):
readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
-
$0
results in./funfacts/es-fun-facts-splashscreens.sh
-
dirname "./funfacts/es-fun-facts-splashscreens.sh"
results in./funfacts
-
cd "./funfacts" && pwd
results in/FULL/PATH/TO/src/funfacts
I hope I was clear. :)
EDIT: if you want an extremely detailed and strong way to get the script's dir, take a look here: http://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
It's a bit overkill for such a single task, but I would like to let the link here for future references. :)
-
-
Hey, thanks again! It's crystal clear.
-
For the fun facts splashscreens project I want to know which theme is currently active in EmulationStation and its font.
Here's what I'm doing:
function get_current_theme() { grep "name=\"ThemeSet\"" "$home/.emulationstation/es_settings.cfg" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p" } function get_theme_font() { xmlstarlet sel -t -v "/theme/view[contains(@name,'detailed')]/textlist/fontPath" "$ES_DIR/themes/$current_theme/$current_theme.xml" 2> /dev/null }
It's working but I don't know if it's the right way to do it.
Acutally,
get_theme_font()
is taken from @meleu 'sgenerate-launching-images
so I guess it should be ok :P -
@hiulit I think they are just fine. Maybe
get_current_theme()
can be tweaked a little to be an one-liner sed. But as this is a small script, it's fine to keep it as is.Another little thing I noticed is regarding that Regular Expression in the
sed
line. You don't need to explicitly match "start of the line followed by anything" (this is what^.*
means).Also, I think we can tweak
get_theme_font()
in order to let it get the font for an arbitrary theme (defined by the user). -
@hiulit said in shell scripting topic:
I found that if I specify a font when using convert it decreases the image creation time, like a lot!
It also depends on the sentence's length. But when specifying a font I get pretty good results with a 3 line sentence :OThat was my first suggestion. But convert gots 2 caveeats
- It needs ghostscript (I'm unsure if caption needs it)
- You have to set correct position
- (It needs the font you call)
... but it's faster than just use captions because it's independent of the images you merge but it's your turn to set coordinates ;)
@meleu It's always a pleasure to see you in action ;)
-
@hiulit said in Fun Facts Splashscreens:
@meleu When I encountered the problem with Pixel theme and
xmlstarlet
I tried a different approach for finding the font.function get_theme_font() { get_current_theme if [[ -n "$(find "$ES_DIR/themes/$current_theme/art" -type f -name '*.ttf')" ]]; then font="$(find "$ES_DIR/themes/$current_theme/art" -type f -name '*.ttf')" else font=$DEFAULT_FONT fi }
I did the job, but I wasn't happy about repeating the
find
function , but I don't know any better :PDo you think that could be a workaround for finding theme fonts?
I know that some themes can have multiple fonts but I think it's ok to use the first one found.
3 notes about the code above:
-
you should use that
font="$(find...)"
before theif
and thenif [[ -n "$font" ]]
(optmization) -
maybe using
find
can be a good approach but only after the current method fail. Also thatfind
command needs more tweaking... Look the files at ComicBook/art directory for example. There are more than one.ttf
file. Then we can make thefind
get only the first match. You did a good catching here! ;) -
that line with
font=$DEFAULT_FONT
... Man, memorize one thing for you shell script coder life: unless you have a really good reason, ALWAYS DOUBLE QUOTE YOUR VARIABLES. If you want an extensive (and a bit boring) explanation for that, you can read it here
I'm on my lunch time now. I'll try to eat something and then send another PR.
Cheers!
-
-
-
@cyperghost as far as I understood @hiulit wants specifically the font used in detailed view. At least that's what that
xmlstarlet
command is getting.@hiulit submitted another PR: https://github.com/hiulit/es-fun-facts-splashscreens/pull/2
-
@meleu Ah okay approved. But I think a generic solution is hard to find - so it's nice to have the fallback option to use Carbon-fonts :)
-
Well, actually, I used the same line @meleu is using in his
generate-launching-images.sh
:P See:xml_path="/theme/view[contains(@name,'detailed')]/textlist/fontPath"
I don't know if we could have a more "standard" search function for fonts, because some themes have different fonts and it would be difficult to know which one is the "primary" font. I'm not into EmulationStation theming so I don't know if there's a predominant font that we could use.
Also, @cyperghost , if I understood you correctly, you though about searching for the font based on the theme's name? (e.g
Pixel theme
->pixel.ttf
)
Because, for what I see, there isn't a "standardized" naming convention on font naming for themes.
The default to Carbon's font it's nice, yeah! ;) -
I would like to try to run Fun Facts Splashscreens on system's shutdown (or startup) but I don't have any clue about where to start looking. Any hint?
I've been looking at this thread https://retropie.org.uk/forum/topic/4836/emulationstation-shutdown-script/ and I've been able to add a line in
/opt/retropie/supplementary/emulationstation/emulationstation.sh
to launches-fun-facts-splashscreens.sh
and it works, but I don't think this is right place to do it nor the way to do it.The place isn't right because, I think, the script should run either on system's startup or shutdown, as @Zigurana mentioned, not when EmulationStation starts, restarts or shutdowns. It doesn't make sense, because the splashscreens shows before EmulationStation starts.
And the way to do it isn't right because I don't like that, for this script to be launched, you have to edit some "core" files.
Is there any way to launch a script either on system's startup or shutdown?
-
@hiulit said in shell scripting topic:
/opt/retropie/supplementary/emulationstation/emulationstation.sh
This file will be overwritten after the next ES update. Then, you're right, it's not the best script to tweak...
Is there any way to launch a script either on system's startup or shutdown?
At shutdown it's a bit tricky. I did this for my trick to gracefully finish ES and save metadata in every system shutdown
At start it's pretty simple:
/etc/rc.local
. And in my opinion your script can safely be placed here but you need to remove that command to show the image in the end.It's a good place because the script can be setted to run in background (add
&
in the end of line that is calling your script). I think the current splashscreen to be displayed will be the previous generated one, but your script would be generating a new splash for the next boot. ;-) -
@meleu I've added the script to
etc/rc.local
but it seems that there's something wrong with the$home
variable. I get this error:sed: cant' read /root/.emulationstation/es_settings.cfg: No such file or directory
Instead of getting
/home/pi
as$home
, it getsroot
, so the script doesn't work. -
@hiulit If I launch the script adding
-H -u pi
it works, but I don't think it's the best way to do it... -
@hiulit said in shell scripting topic:
@hiulit If a launch the script adding
-H -u pi
it works, but I don't think it's the best way to do it...IMHO it's perfectly acceptable and also a really good way to do it. ;-)
Of course if we evolve this method, this config (adding the script call in
rc.local
) won't be made manually, and then we can get the username to use.;)
-
@meleu But then, does it make sense to keep using this?
user="$SUDO_USER" [[ -z "$user" ]] && user="$(id -un)" home="$(eval echo ~$user)"
Because that's what's getting
root
as$home
when the script is launched in/etc/rc.local
.We could then just do
home="/home/pi"
, don't you think?
Because adding the user-H -u pi
like this removes the dynamism of the function (getting the user dynamically), right? -
@hiulit said in shell scripting topic:
@meleu But then, does it make sense to keep using this?
user="$SUDO_USER" [[ -z "$user" ]] && user="$(id -un)" home="$(eval echo ~$user)"
Yes. Keeping this in the script makes sense because the user can use it at any moment, right?
Because that's what's getting
root
as$home
when the script is launched in/etc/rc.local
.Does it happen even when using
sudo -H -u pi ...
???We could then just do
home="/home/pi"
, don't you think?
Because adding the user-H -u pi
like this removes the dynamism of the function (getting the user dynamically), right?Sorry if I misunderstood you, but you want to avoid to remove dynamism by hardcoding
home=/home/pi
? Sounds weird for me.I would consider what we have until now like a proof-of-concept script (kudos to you for working on this). Let's wait what the big boss will say about my proposal and take next steps based on his decision.
If he refuse, we can continue this project extra-officially. I'm just trying to say that what approach to use to launch the script at startup depends on that decision.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.