Find the answer to your Linux question:
Results 1 to 6 of 6
I'm using the following lines as part of a script to get me a random wallpaper slideshow: Code: LOCATION=~/.kde/share/wallpapers Background=$(ls ${LOCATION}) background=($Background) num_background=${#background[*]} Esetroot -fit ${LOCATION}/${background[$((RANDOM%num_background))]} I've copied it from ...
  1. #1
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211

    [newbieness] recursive directory listing as a variable? [solved]

    I'm using the following lines as part of a script to get me a random wallpaper slideshow:

    Code:
    LOCATION=~/.kde/share/wallpapers
    Background=$(ls ${LOCATION})
    background=($Background)                
    num_background=${#background[*]}       
    Esetroot -fit ${LOCATION}/${background[$((RANDOM%num_background))]}
    I've copied it from somewhere, maybe from here.

    It works fine, but I'd like to organize my wallpapers in many folders, both to be organized and also to solve a little bit the problem of a list with too many files when manually setting the wallpaper on fluxbox menu, as I would have the list split into smaller sublists.

    So, is there some way to make this "location" variable to be recursive?

    A sort of workaround I've thought would be to have organized real files in folders and another folder with symbolic links (or something like that) to each file, or perhaps some sort of "meta folder" whose content would be automatically the content of a certain list of folders or something like that, not requiring a link to each file individually. I suppose both of these things can be done, but I don't know how to do any.

    edit: I've figured how to make links to individual files, but I think that's the worst solution possible and I don't really learn anything valuable...
    Last edited by the dsc; 07-02-2007 at 05:19 PM. Reason: solved

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    Best way to make it recursive would probably be to use the "find" command instead of the "ls" command eg: change:

    Code:
    Background=${ls ${LOCATION}}
    to:
    Code:
    Background=${find ${location} -type f -iname '*'}
    You can change the -iname variable to only include jpg's or only png's etc eg: "-iname '*jpg'"

    HTH
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211
    Thanks! The scheme you presented didn't work, but made me realize that there is this "ls" that is just the same thing as the command line "ls" ( ), so I used "ls -R" which stands for recursive, and it did work


    Is there any command line I could use to just retrieve which value a given variable has at a time?

    More or less like, if I were in basic (language), typing "print $Background" and pressing enter...

  4. #4
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  5. #5
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211
    Thanks guys.

    But just in case sdousley went a bit surprised with my simpler "solution", is because it doesn't really work as I thought.

    I've made a few file shortcuts and forgot to delete them, so I thought I was getting files from sudirectories, when, instead, I'm getting only filenames but not the full path, which seems to be impossible only with "ls", and is needed to the whole thing work. I've searching a little bit and the way to emulate that seems to be using "find".

  6. #6
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    211
    SOLVED!

    The problem was that I should have used parenthesis "externaly" and curly brackets within, I think. Or at least I did that with sdousley's suggested solution and now it definately works.

    Before it was giving an error message, "bad substitution", and eventually I noticed that difference with brackets and parenthesis, not that even crossed my mind that it may have anything to do with "bad substitution", but seemed logical and harmless, then I tried.



    A final note:

    another modification needed is removing the "location" part after the esetroot command, or else you'll get a redundance, part of the file address will appear twice in the variable. In my case, instead of getting something like:

    /.kde/share/wallpapers/landscapes/1.jpg

    I'd get:

    /.kde/share/wallpapers//.kde/share/wallpapers/landscapes/1.jpg

    Or something like that.

    The odd thing is that when I first edited to fix only the bracket issue, it worked anyway, despite of that, somehow... but after I restarted fluxbox, no wallpaper appeared, and I tested the script through a terminal, where the problem became evident.

    Summarizing, the last line should be:

    Esetroot -fit ${background[$((RANDOM%num_background))]}



Posting Permissions

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