2014-03-31

Linux Script sample

I spent part of the morning trying to figure out a problem on a Linux machine.

The problem is
  1. I like the desktop background to switch among several nice backgrounds in a folder of pictures that I keep
  2. The XFCE desktop environment I'm running does this
  3. The XFCE desktop environment keeps a list of backgrounds in a text file.
    This file usually appears in ~/.config/xfce4/desktop/backdrop.list
  4. I don't want all the pictures in the folder in item (1) are on the list in item (3), but I want some of the ones I've added to the folder to be added to the list
  5. The whole process in item (4) would be easier if I could generate a list of files that are in the folder but not in the list.
Happily for me, the files I'm considering could be found by searching for '*.jpg' filenames in a specific path.

Anyway, this is the kind of task that ought to be easy in a scripting language. And the Linux environment I'm running has lots of scripting languages available.

I tinkered with the results of a simple find command, not liking the problems I ran into trying to execute a grep and parse the results.

Then I realized that what I wanted to do, I could do in a for loop.

I needed to use a simple find command to generate a list of files, and use the loop to look for each filename in the backdrop-list file. If found, I would print out one thing. If not found, I would print out something else.

This loop looks like this, typed in all at once on a single command line:
sj@hostname ~ $ for i in `find ./pathTo/pictureFolder/ -iname "*.jpg"`; do if grep -q $i ~/.config/xfce4/desktop/backgrop.list ; then echo "Found file $i"; else echo "Did not find file $i"; fi; done;
While a little complex to type in, this does the trick.

But I realized that I wanted to be able to repeat this, and I wanted to store the resulting lists of found and not-found files.

I decided to turn the complex command into a script file. I also decided to put the results into a pair of files in the /tmp folder.
#!/bin/bash

if [[ -e /tmp/pic-found ]] ;
 then
   rm /tmp/pic-found;
fi   

if [[ -e /tmp/pic-notfound ]] ;
  then
    rm /tmp/pic-notfound;
fi

for i in `find ./pathTo/pictureFolder/ -iname "*.jpg"`;
  do
  if grep -q $i ~/.config/xfce4/desktop/backdrop.list ;
    then
      echo $i >> /tmp/pic-found;
    else
      echo $i >> /tmp/pic-notfound;
  fi;
  done;

sort /tmp/pic-found > /tmp/pic-found_sort
sort /tmp/pic-notfound > /tmp/pic-notfound_sort

It's not beautiful, but it does what I want.

And, when I want to spend an hour browsing through pictures to add to the background-image list tonight, I'll have a much better idea of where to start looking. Instead of spending time looking at the 200-or-so pictures I've already added, I'll be able to look at the ones that haven't been added yet.

It's a small thing, but a little scripting reduced my work-load. And by more time than I spent working on the script.

No comments:

Post a Comment

I like thoughtful feedback; I prefer polite feedback.

I don't like screeds.

Comments older than a few days will have comments go into moderation.