[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

Basic bash Linux - display random gif from directory on timer, then cycle to next random gif - loop forever

Experts,

In conjunction with this utility (http://www.imagemagick.org/) to display gifs; I'd like to combine it with a script that's on a continual loop and cycles through random gifs.  I've got a directory with TONs of gifs, and I'd like to launch the script on start up, and then continue until the end of time.

Is this at all possible, and if it is could somebody point me in the right direction on where to learn how to do this?

Any assistance will be greatly appreciated.  This is for a non-gui Linux distro.
0
usslindstrom
Asked:
usslindstrom
  • 5
  • 2
1 Solution
 
Frosty555Commented:
Where exactly are the GIFs being displayed? In a client's webbrowser? In a desktop window on the Linux machine?
0
 
nemws1Commented:
Can the images repeat within a loop (ie truly random image each time) or should each image only be displayed once until *all* images have been displayed, then it can be displayed again?
0
 
nemws1Commented:
If you have GNU's sort installed, you can use the '-R' option to sort randomly.  This assumes you want the latter of my described situations (display all in random order before any repeats).  Also, the ImageMagick 'display' option displays the image until you quit it.  So this does *not* run automatically forever.

#!/bin/bash
IMGDIR=/path/to/your/images

while :
do
        IMAGES=`/bin/ls $IMGDIR | sort -R`
        for img in $IMAGES
        do
                display "$IMGDIR/$img"
        done
done

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nemws1Commented:
If you want zero interaction, this will kill 'display' after DISPLAY_TIME seconds.
#!/bin/bash
IMGDIR=/path/to/your/images
DISPLAY_TIME=10

while :
do
    IMAGES=`/bin/ls $IMGDIR | sort -R`
    for img in $IMAGES
    do
        display "$IMGDIR/$img" &
        DISPLAY_PID=$!
        sleep $DISPLAY_TIME
        kill $DISPLAY_PID
    done
done

Open in new window

0
 
usslindstromAuthor Commented:
Wow - these posts are great!  - Give me a minute to take them in and try to understand them.

-- Where exactly are the GIFs being displayed? In a client's webbrowser? In a desktop window on the Linux machine?
  This is on the local client machine only.  This is actually from the use of a Raspberry Pi w/2.5" screen attached, mounted to the front of a box I threw together.

-- Can the images repeat within a loop (ie truly random image each time) or should each image only be displayed once until *all* images have been displayed, then it can be displayed again?
   Truthfully, it doesn't matter in this instance, truly random or sequenced through all prior to going through it again.  Your script looks like it will accomplish this flawlessly.  I'm fairly decent in vbs, so I get the basics of what you're showing me.  Bash can be a bit of a bear to understand though.

If you don't mind me asking, the while loop here, you didn't set a conditional statement, so that basically means it will never terminate?  (If so, that's pretty wild.  :) )
0
 
nemws1Commented:
Images are displayed in a window on the Linux desktop - you mentioned the ImageMagick package, which has a 'display' program and that's what that script uses.

"while :" basically means the condition "true" is "true", which is always the case. :)
0
 
nemws1Commented:
BTW - here's a great use of 'while :' for a quick and dirty clock.  You should know, to break out of these loops, do a CTRL-C.

#!/bin/bash
while :
do
    date
    sleep 1
done

Open in new window

0
 
usslindstromAuthor Commented:
Outstanding, thank you very much for the solution, as well as explination.  Much appreciated!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now