LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-26-2017, 10:02 PM   #1
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Rep: Reputation: 15
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
 
Old 07-26-2017, 10:09 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,293
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
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.
 
Old 07-26-2017, 11:00 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 07-27-2017, 12:52 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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)
 
Old 07-27-2017, 08:57 AM   #5
compused
Member
 
Registered: Oct 2006
Location: Melbourne Australia
Distribution: centos and redhat 8
Posts: 91

Original Poster
Rep: Reputation: 15
thanks a lot guys...those suggestions seem to have fixed it...
 
Old 07-28-2017, 12:19 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can you nest backticks? BrianK Programming 10 07-18-2023 06:49 PM
LXer: Lavabit goes head to head with feds in contempt-of-court case LXer Syndicated Linux News 0 01-29-2014 01:21 PM
LXer: Head to Head Desktop Comparison: Ubuntu 12.10 vs OS X Mountain Lion LXer Syndicated Linux News 0 01-24-2013 03:40 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
x86 Solaris 9 XSun and Matrox G550 dual-head... one head down, one to go. finegan Solaris / OpenSolaris 4 03-11-2003 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration