Shell script, how to add variable filename to path?

Hi all, I’ve been bashing(!) my head against a wall for the past few days trying to figure this simple issue out; I want to use a bash script (called by an Exec node in Node Red) to simply copy a certain jpg to a datestamped filename. My IP cam’s stream is being processed by Motion, and automatically creates a “lastsnap,jpg” which gets overwritten every 10mins, so I just want to copy it and name the copy in a way that it isn’t overwritten and stays in chronological order.

I have a script working, where if I hard-code the filename it works fine, eg.

cp /var/lib/motion/cam2/lastsnap.jpg /var/lib/motion/cam2/lastsnap_copy.jpg

but obviously this also gets overwritten every time it copies. I want to specify a variable filename using date, but everything I try either gets ignored or ends up with gobbledegook, something along the lines of “LNOS~.jpg”
I want to use the format of

date +%F-%H:%M

I’ve tested this in the command line and it gets the format correctly, but I can’t for the life of me figure out how to get a variable to work in the shell script. Any examples I’ve found and tried, with various combinations of $, brackets, apostrophes, quotes etc., all end up either being ignored or getting seemingly unrelated filenames. I’m thinking it may have something to do with trying to concatenate the variable into the path and filename?
My googlefu has failed me, I need your help! Any assistance greatly appreciated!

  • Platform information:
    • Hardware: RPi 3
    • OS: raspbian stretch
    • openHAB version: 2.4

It’s not really openhab related…
anyway try this

NOW=$(date +"%F-%H:%M")

/bin/cp /var/lib/motion/cam2/lastsnap.jpg /var/lib/motion/cam2/lastsnap_$NOW.jpg

Hi alexxio, I’d tried all sorts of info from various linux sites and couldn’t get anything to work so was wondering if there was something special that I needed to do in openhabian? I tried your exact code, which is very similar to several versions I’ve tried before, and the resulting file was named “LYRRR8~A.JPG”??
Any clues as to why I’d be getting results like this when everything indicates it’s the correct code? Note that even the extension is uppercase.

Try to change this line
NOW=$(date +"%F-%H:%M")


NOW=$(/bin/date +"%F-%H:%M")

Have you some peculiar installation? Eg. Docker?
In a standard pi system the binaries should be available…other things to check are openhab user permissions to that folder, file and all the commands you’re issueing (cp, date).

From the commandline thry which bash or whereis bash
Some distributions have it in /usr/bin I believe. I agree it is a Linux distribution -specific question.

/bin/date gives the same result. which bash gives /bin/bash
Running a stock-standard openhabian SD card install, the linux distribution is openhab’s own so not sure why it’s not permitted to ask here; in trying to track the problem down I have seen plenty of other similar questions asked and answered here.
I’ve checked permissions, even run the script as sudo and still get the same result. cp and date both work correctly, and both can be called from anywhere in the filesystem or specifically from the /bin/ directory, but for some reason anything to do with trying to use a variable does not work. I tried using lastsnap_$1.jpg instead to see if that would do anything, and ended up with “lastsnap_.jpg”.
I’ve tried setting plain text as the variable contents and still get the same issue, so it’s not just when trying to something more complicated like date; plain text also results in the random uppercase lettering with tilde; note that the actual jpg content is correctly copied, it’s only the filename that is going awry.

OpenHABian is just a Raspbian Lite distribution. What version depends on the OpenHAB image version.
I tend to install Raspbian Lite directly and then install OpenHABian.
You can look at the contents of /etc/os-release to see the version. I am one version behind the latest.

VERSION="9 (stretch)"

OK this is bizarre, I tried stripping things back to basics by assigning
and that worked. Reinstated just date (no formatting), and THAT worked. Added bits of formatting and it died when adding “:” before minutes, took that away and I’ve now got a usable script result.
The thing is I’ve done this DOZENS of times over the past few days, and absolutely nothing has changed in the code I’ve been testing between what failed previously, even ten minutes ago, and what is now working. No spelling mistakes, blank spaces, permissions, nothing, but now it’s decided it will work.

I need a drink. Thanks guys, though I still have no idea why it’s working now and it wasn’t for the past several days of trying the exact same thing.

If it appears flaky, try escaping the + sign like this /bin/date \+"%F-%H:%M"