Results 1 to 10 of 10

Thread: Simple bash script question

  1. #1
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Simple bash script question

    I am currently using this script in kde to change my wallpaper to a random wallpaper and it works for the most part. (Original script found on appleInsider.com)

    I'd like to snag only .png and .jpg out of the $dir.
    The commented line illustrates what I'm trying to accomplish, with a generic regular expression. Using the commented line, the script breaks.

    Any suggestions are appreciated.


    Code:
    #!/bin/bash
    dir=/usr/share/wallpapers/
    
    pics=(${dir}*)
    #pics=$(ls $dir | grep [.]...$)
    
    numpics=${#pics}
    img=${pics[$((RANDOM%numpics))]}
    dcop kdesktop KBackgroundIface setWallpaper $img 6

  2. #2
    Join Date
    Apr 2006
    Beans
    580
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Simple bash script question

    I think you should just be able to do this:

    Code:
     pics=(${dir}*.[pPjJ][nNpP][gG])
    I think.

  3. #3
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Simple bash script question

    It's always the simple stuff that gets overlooked.

    That did the trick, thanks.

  4. #4
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    481
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Simple bash script question

    You can use
    pics=(${dir}*.{png,jpg})
    to match a list of specific suffixes.

    The
    numpics=${#pics}
    line looks wrong. That evaluates to the number of chars in $pics[0].
    You want
    numpics=${#pics[@]}
    to evaluate to the number of elements in the "pics" array.

  5. #5
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Simple bash script question

    Current script:

    Code:
    #!/bin/bash
    dir=/usr/share/wallpapers/
    pics=(${dir}*.{png,jpg})
    numpics=${#pics[@]}
    img=${pics[$((RANDOM%numpics))]}
    curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)
    
    if [ $img = $curImg ]; then
            change_wallpaper #script name
            exit
    fi
    dcop kdesktop KBackgroundIface setWallpaper $img 6
    Only other change I see is the 1 in this line to "currentDesktopNumber."
    Code:
    curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)

  6. #6
    Join Date
    Mar 2005
    Location
    Texas
    Beans
    1,675
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Simple bash script question

    Am i missing something, or can you not just right click the desktop, select slide show, and check the "random" box?
    100 buckets of bits on the bus,
    100 buckets of bits,
    Take one down, short it to ground,
    FF buckets of bits on the bus.

  7. #7
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Simple bash script question

    Random on demand, not on a set time.

  8. #8
    Join Date
    Mar 2005
    Location
    Texas
    Beans
    1,675
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Simple bash script question

    Okay just checking
    100 buckets of bits on the bus,
    100 buckets of bits,
    Take one down, short it to ground,
    FF buckets of bits on the bus.

  9. #9
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Simple bash script question



    Originally, I was using the KDE built-in functionality, but with a script like this, any new images are automatically added when they are saved to the $dir.

    Then if you cron the script, it's just a little more versatile version of what currently exists. IMO

  10. #10
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Simple bash script question

    Most recent version

    Code:
      GNU nano 2.0.6       File: /home/ac/bin/change_wallpaper
    
    #!/bin/bash
    function getNewPaper {
            dir=/usr/share/wallpapers/
            pics=(${dir}*.{png,jpg})
            numpics=${#pics[@]}
            img=${pics[$((RANDOM%numpics))]}
    }
    
    function getCurPaper {
            curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)
    }
    
    while [ $img = $curImg ]
    do
            getNewPaper
            getCurPaper
    done
    
    dcop kdesktop KBackgroundIface setWallpaper $img 6
    
    exit 0
    Going to add a counter to the while at some point to avoid an infinite loop.
    Last edited by harold4; November 12th, 2007 at 10:51 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •