LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   cron not liking ?backticks, ?pipes ?head exp (https://www.linuxquestions.org/questions/linux-software-2/cron-not-liking-backticks-pipes-head-exp-4175610691/)

compused 07-26-2017 10:02 PM

cron not liking ?backticks, ?pipes ?head exp
 
I am running this script via an .sh file within cron:

/bin/sh /path/to/cp -rp "`var=$(ls -dt -- /folder-one/folder-two/*/ | head -n1); var="${var%??}"; echo "$var"`" "/folder-three/folder-four/"

the script is designed to copy the latest folder from the conents of /folder-one/folder-two/*

anyone can help?
thanks
compused

Turbocapitalist 07-26-2017 10:09 PM

I would use only the $( ... ) notation for command substitution and not the backticks. It makes it much easier to read.

But the actual problem is probably coming from the quotes. You have double quotes inside your double quotes and the inner sets need to be escaped.

AwesomeMachine 07-26-2017 11:00 PM

Usually you put everything in the script, and then just put the name of the script in the cron job. But make sure you use full paths, because cron uses a different environment than the command line in a virtual terminal.

pan64 07-27-2017 12:52 AM

yes, it is overcomplicated. It is not the cron who does not like it, but the human beeing who cannot construct such a complex command in one single line.
As it was suggested better to put it all in a script and you invoke only the script from cron.
Furthermore I would try to simplify it:
Code:

# for example, instead of
var="${var%??}"; echo "$var"
# you can use:
echo "${var%??}"

also $( ) is much better than backtick (as it was mentioned)

compused 07-27-2017 08:57 AM

thanks a lot guys...those suggestions seem to have fixed it...

pan64 07-28-2017 12:19 AM

glad to help you.
If you think your problem is solved please mark the thread solved.
If you want to say thanks just click on yes.


All times are GMT -5. The time now is 01:48 PM.