0

On a Raspberry, I made a custom slideshow with movies and pictures. I am expanding the script so I can also choose a clip to play if necessary. I made a menu with 3 options:

  1. Slideshow
  2. Clips
  3. Shutdown

The Slideshow is an infinite loop:

case $opt in
            "Slideshow")
            PLAY="1"
            while true; do
                    if [ "$PLAY" = "1" ];
                    then
                            PLAY=2
                            bash ./videoplayer.sh
                    else
                            PLAY=1
                            bash ./pictureplayer.sh
                    fi
            done
            ;;

The example of videoplayer.sh:

VIDEOPATH="/home/pi/ftp/video"

for entry in $VIDEOPATH/*
do
        clear
        omxplayer "$entry" > /dev/null
done

The example of pictureplayer.sh:

PICTUREPATH="/home/pi/ftp/picture"

fbi -a --noverbose -t 30 -u -1 $PICTUREPATH/*.png

At any given moment I would like to stop the while loop and show the menu again. Even if it is in the middle of a clip playing. I only found options that can read an input after or before every play of a clip/picture.

ErXoR
  • 3
  • 3

1 Answers1

0

You have two issues: stopping the clip and, if you want instant response, accepting keys instead of input lines. Run the slideshow in the background and then kill it after the read returns:

case $opt in
            "Slideshow")
            (set -m             # set up process groups
             ./slideshow.sh &   # the existing loop
             read -n1
             kill -HUP %+)      # subshell suppresses signal reporting
            ;;

The one kill affects all the processes (demo) because a shell with job control (-m) enabled puts them all in a process group. (Note that you don't have to put bash in front of a shell script name if you give the file a shebang and make it executable.)

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Thanks for the answer. I had to search a bit. From what I found and tried so far this option works: I added a new option 3) Exit Current Play and did a pkill -f slideshow.sh, pkill -f videoplayer.sh and a pkill -f omxplayer. I added in the first option a "&" after bash ./slideshow.sh. – ErXoR Sep 22 '17 at 14:31
  • I added a bit more text as an alternative to the above (which may very well work). – Davis Herring Sep 23 '17 at 05:25
  • I added the new code in the original post. The last code you gave me didn't work like it supposed to. it didn't stop the script. I got a new small problem now, it's stated in the original post. – ErXoR Sep 24 '17 at 10:31
  • @ErXoR: You should probably ask a new question rather than editing this one into being altogether different. But what do you mean by the generic "didn't work"? – Davis Herring Sep 24 '17 at 13:22
  • Will do so, I will remove the editing in the first post. The code that you gave (read -n1 and kill -HUP $!) doesn't stop the slideshow. It just keeps going. The pkill -f command works in this case. Could it be that it keeps going because I'm not putting the commands directly into the terminal, but through ssh? – ErXoR Sep 24 '17 at 13:27
  • @ErXoR: Sorry: I misread the very manual page I linked to! The correct answer is to use process groups (partly because it avoids needing to know the names of the subprocesses); I've edited the answer. – Davis Herring Sep 24 '17 at 16:10