There's a number of characters in file names that would make that fail. You can improve it with:
#! /bin/sh -
cd /home/pi/JPGS || exit
fn=$(ls -t | head -n1)
mv -f -- "$fn" /home/pi/WWW/webpic.jpg
Leaving a variable unquoted in list context (in Bourne-like shells other than zsh
) is the split+glob operator, you almost never want to do that. --
marks the end of the options so "$fn"
will not be taken as an option if it starts with -
.
That still fails if filenames contain newline characters, but not space, tab, star, question mark, right square bracket, or start with dash.
Best is to use zsh
here:
#! /bin/zsh -
mv -f /home/pi/JPGS/*.jpg(.om[1]) /home/pi/WWW/webpic.jpg
(.om[1])
are glob qualifiers, they are a zsh
specific feature. .
restricts the glob to regular files (will not include symlinks, directories, devices...), om
is to order on modification time, and [1]
to only take the first file.
Note that if you want to assign that to a shell variable, that would have to be an array variable:
fn=(/home/pi/JPGS/*.jpg(.om[1]))
(not that it makes a lot of difference on how you use it later).
--
and why is zsh preferable over bash here? I'm also not sure(.om[1])
does. – Levon Dec 21 '13 at 21:57mv $f xx
which is the wrong syntax and bad practice (and where most of the shell-related vulnerabilities come from) butmv -- "$f" xx
– Stéphane Chazelas Dec 22 '13 at 13:58--
which is clearly important from a security standpoint too - thanks again – Levon Dec 22 '13 at 14:13