Looking for a script that copies files only on Sundays

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
divisionmd
Posts: 606
Joined: Sat 14 Jul 2007, 20:42

Looking for a script that copies files only on Sundays

#1 Post by divisionmd »

Hello,

- Anyone handy with bash script can do this:

- from one line: if its sunday -> copy a file from A to B -> if not sunday continue script

easy to do? was thinking of a few crontab methods but interesting to hear what other smart ways there is to do this?

Thanks,

Best regards,
Johan

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#2 Post by GustavoYz »

If I'm getting the idea, something like this shuold work:

Code: Select all

if [ $( date | awk '{print $1}' )="sun" ]; then
   cp $A $B && echo "Copied $A... OK"
else
   # rest of the script
fi
Shorter way:

Code: Select all

[ $( date | awk '{print $1}' )="sun" ] && cp $A $B || ./script.sh

User avatar
divisionmd
Posts: 606
Joined: Sat 14 Jul 2007, 20:42

#3 Post by divisionmd »

Hello GustavoYz,

Thanks!

What about this one: copy A to B on everyday except sunday?

thansk for help,

Best regards,
Johan

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#4 Post by GustavoYz »

Hi divisionmd,
Would be the same:

Code: Select all

if [  $( date | awk '{print $1}' )="sun" ]; then 
     # something to do only in sundays
 else 
     # the rest of the week:
     cp $A $B && echo "Copied $A... OK" 
 fi

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#5 Post by Moose On The Loose »

GustavoYz wrote:Hi divisionmd,
Would be the same:

Code: Select all

if [  $( date | awk '{print $1}' )="sun" ]; then 
     # something to do only in sundays
 else 
     # the rest of the week:
     cp $A $B && echo "Copied $A... OK" 
 fi

Beware of case. I get:
Tue Mar 5 07:13:27 PST 2013

The first letter is upper case.

To easily ignore case, use grep

Code: Select all

# if (date | grep -q -i "^tue" ) ; then echo "Works today"; fi
Works today
# if (date | grep -q -i "^wed" ) ; then echo "Works today"; fi
# 

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#6 Post by L18L »

date --help wrote:Usage: date [OPTION]... [+FORMAT]
...
FORMAT controls the output. The only valid option for the second form
specifies Coordinated Universal Time. Interpreted sequences are:
...
%u day of week (1..7); 1 is Monday

Code: Select all

if [ $(date +%u) -eq 7 ] ; then echo copy_files; else echo no sunday ; fi
# if [ $(date +%u) -eq 7 ] ; then echo sunday; else echo no sunday ; fi
no sunday
#
Feel free to continue this test on next Sunday :D

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#7 Post by GustavoYz »

Yep, I use Spanish date format, so is all an aproximation.
But the idea is clear, no "cron-magic" is really needed, with date you're on.

Post Reply