If I am in (so my presentWorkingDir is) a dir that has a *.jpg or *.png or *.jpeg filetype then the wildcards expand and bug out the find command with the following error:
find: paths must precede expression: linuxVSvista.jpg
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
/home/knoppies/scripts/wallswitch.sh: line 64: ( 12462 % 0 ) : division by 0 (error token is ") ")
linuxVSvista.jpg is the second image I have in wallpapers, If I am in a subdir, then it tends to pick the second image of that subdir)
I know that division by 0 is caused because find returns an error, rather than pictures.
Does anybody know of possible causes/solutions? I have fiddled with it and I am unable to work it out.
EDIT: I have worked something out: bash/find tends to auto-expand *.jpg like so:
++ find /home/knoppies/Pictures/wallpapers/cars/ '(' -iname '*.png' -or -iname fireSkellaton.jpg linuxVSvista.jpg noise-brands-3450.jpg turntable.jpg xbmcwallpaper.jpg -or -iname '*.jpeg' ')' '!' -type d
To fix the problem I used "cd /" to change my working dir to one that I am certain exists, and almost certain contains no .jpg/,png/.jpeg files in it.
My new code (the following code has been fixed of the above error, and should work):
Code:
#!/bin/bash
# add -x to the shabang for some more debug info.
# http://ubuntuforums.org/showthread.php?p=10602790
# WARNING: This script does no sanity checks on CLI arguments: Garbage in=Garbage out
# Known bugs:
# I find that sometimes after the script runs it doesn't display a Picture, so all I get is a plain purple background (the Gnome default background colour I think). I think this has more to do with my file structure than the script. I suspect that if it chooses a file in a Directory that has a space in the name, or a file with a space in the name, then the script fails. After cleaning up my wallpapers directory/file names, I no longer get any 'purple backgrounds'
# CLI Arguments:
# -d folder/ : sets directory(relative to working dir) to search for wallpapers.
# -D folder/ : sets directory(absolute) to search for wallpapers.
# -w folder/ : sets directory(relative to wallpapers) to search for wallpapers.
# -m #: Sets the maxdepth of the recursive search.
# -r #: recursive. (default)
# -R : non-recursive. Sets the recursive depth to 1.
# -t #: specifies the time between changes, in seconds. Loops indefinitely
# -h : TODO: prints help text.
# -f to disable globbing
# -f
# KILL previous script processes
name=( $( pgrep "wallswitch.sh" ) )
for (( i = 0 ; i < ${#name[@]} - 1 ; i++ )); do
kill -9 ${name[$i]}
done
DIR="$HOME/Pictures/wallpapers"
depth=""
verbose=0
option="zoom"
# I need to implement some type of help.
# catch command line FLAGS (Please understand that there are no 'fail safes' for incorrect arguments.)
while getopts ":d:D:w:m:rRt:hvo:" o; do
case $o in
d) # TODO: if first character is / then (absolute) else
DIR=`pwd`/$(echo "${OPTARG}" | sed -e "s/\/*$//");; # set DIR(presentWorkingDir) & cut trailing slash
#fi
#;;
D) DIR=$(echo "${OPTARG}" | sed -e "s/\/*$//");; # set Dir(absolute) & cut trailing slash
w) DIR=$HOME/Pictures/wallpapers/$(echo "${OPTARG}" | sed -e "s/\/*$//");; #set DIR (wallpapers) & cut trailing slash
m) depth="-maxdepth $OPTARG";; # Set MaxDepth
R) depth="-maxdepth 1";; # Set MaxDepth=1, no recursive
r) depth="";; # Searches files recursively (default behaviour)
t) time=$OPTARG;; # sleep time in sec; loops indefinitely.
v) verbose="1";; # OBVIOUSLY sets verbose to true. Prints out the chosenPic:
o) option=$OPTARG;;
h) # echo "I should echo some command line help here."
default) echo "Usage: $0 [-d path -D path -w path -m # -r -R -t # -h -v -o option"
echo "-d path (relative from present working dir (pwd) )"
echo "-D path (absolute)"
echo "-w path (relative from $HOME/Pictures/wallpapers/ )"
echo "-m # max depth"
echo "-r recursive (default behaviour)"
echo "-R non-recursive. sets maxdepth=1"
echo "-t # time (in seconds) between updates. If omitted then updates once and quits"
echo "-h prints this help text"
echo "-v sets verbose to true. Prints out the filename of the file that has been selected."
echo " Please understand that if you have spaces in your filestructure, it will confuse this script, and your wallpaper will return to the default colour"
echo "-o option WHERE option takes on the values: 'none', 'wallpaper', 'centered', 'scaled', 'stretched', 'zoom', 'spanned'."
echo "Created and edited by rocuan AND rothalem (on ubuntuforums)."
echo "http://ubuntuforums.org/showthread.php?p=10602790"
esac
done
cd / # an attempt at fixing a bug. Bash auto-expands *.png *.jpg *.jpeg when find tries to use them as $TYPE. It messes with my script. This (mostly) fixes the issue (assuming / contains no such files)
# echo $verbose # DEBUG
GCONF="gconftool-2 -t str -s /desktop/gnome/background"
TYPE='-iname *.png -or -iname *.jpg -or -iname *.jpeg'
#TYPE='-iname \*.png -or -iname \*.jpg -or -iname \*.jpeg'
#TYPE='-iname "*.png" -or -iname "*.jpg" -or -iname "*.jpeg"'
while true ; do
# echo "find $DIR/ $depth \( $TYPE \) ! -type d" #DEBUG
if [[ -n "${depth}" ]] ; then # specify a maxdepth
new_pic=( $(find $DIR/ $depth \( $TYPE \) ! -type d) )
else
new_pic=( $(find $DIR/ \( $TYPE \) ! -type d) )
fi
random=$[ ( $RANDOM % ${#new_pic[@]} ) ]
# echo "ChosenPic:" #DEBUG
# echo ${new_pic[$random]} #DEBUG
if test ${verbose} -gt 0 ; then # If verbose, print out the chosenPic:
echo "ChosenPic: ${new_pic[$random]}"
fi
$GCONF/picture_filename "${new_pic[$random]}"
$GCONF/picture_options $option # Possible values are "none", "wallpaper", "centered", "scaled", "stretched", "zoom", "spanned". Default = zoom
if [[ -z "${time}" ]] ; then #If time is not set, break loop (so the script finishes).
break
fi
if test ${time} -lt 0 ; then #If time is negative (illegal), break loop (so the script finishes).
echo "ERROR: Time (-t) needs to be set to a positive number (in seconds). EXITING!"
break
fi
sleep $time
done
Bookmarks