LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2015, 03:23 PM   #1
Archangelmscj
LQ Newbie
 
Registered: Jun 2015
Posts: 12

Rep: Reputation: Disabled
Pull my hair out


I have created a bash script to look at backup counts if it exceeds two to delete oldest one. However I keep getting an error "cp: cannot stat `backup1.log': No such file or directory". The directory is valid and root has read, write / user has read, write / others have not permission to the file. I have created another script and it work fine. This new script doesn't..... Below is the new script


backup_dir=/var/CPbackup/backups
log=/root/bin/backup.log
backup_count=`ls|grep ^backup|wc -l`
file_to_delete=`ls|grep ^backup|head -1`
file_to_copy=`ls|grep ^backup`

if [[ ${backup_count} > 2 ]]
then
${file_to_delete}
echo "`date`: Deleting ${file_to_delete}" >> ${log}
rm -f ${file_to_delete}
echo "`date`: File has been deleted." >> ${log}
else
echo "`date`: Mounting cifs Mirror..." >> ${log}
mount -t cifs //10.172.0.223/Mirror$ /mnt/offsite -o credentials=/root/bin/.tarconnect
cd ${backup_dir}
echo "`date`: Copying file_to_copy to WF13WP03..." >> ${log}
cp ${file_to_copy} /mnt/offsite/Offsite\ Mirror/ >> ${log}
echo "`date`: Copy complete unmount cifs from WF13WP08..." >> ${log}
umount /mnt/offsite
fi
 
Old 09-01-2015, 03:31 PM   #2
Ihatewindows522
Member
 
Registered: Oct 2014
Location: Fort Wayne
Distribution: Ubuntu 16.04 LTS
Posts: 614
Blog Entries: 2

Rep: Reputation: 166Reputation: 166
Are you running the script as root?
 
Old 09-01-2015, 03:36 PM   #3
Archangelmscj
LQ Newbie
 
Registered: Jun 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
@Ihatewindows522
Yes I am running the script as root
 
Old 09-01-2015, 04:59 PM   #4
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,385

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Code:
file_to_copy=`ls|grep ^backup`
cd ${backup_dir}
cp ${file_to_copy} /mnt/offsite/Offsite\ Mirror/ >> ${log}
cp: cannot stat `backup1.log': No such file or directory
backup1.log exists in the initial working dir but not in $backup_dir
 
Old 09-01-2015, 05:59 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,943

Rep: Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522
Is the posted script complete?
Is the new one and the posted one identical?

As stated the script will assign variables file_to_delete, file_to_copy and backup_count based upon the current working directory versus the backup directory.
 
Old 09-01-2015, 10:07 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
1. See http://www.tldp.org/LDP/abs/html/comparison-ops.html
Code:
if [[ ${backup_count} > 2 ]]
is string comparison, not integer.

2. Try adding
Code:
set -xv
to the top of your script - it shows you exactly what the parser is doing.
 
Old 09-02-2015, 01:48 PM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,324
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
No solution for you, but you might want to change the ``'s to $(). It's easier to read and does the same thing. And for some things the difference between success and failure.

$ echo `date`
$ echo $(date)
 
  


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
nfs4 is making me pull my hair :-( Whatswithmynick Linux - Server 3 06-04-2015 09:09 AM
NX Anyone as I pull my hair out! RayAID Red Hat 3 12-15-2010 02:50 PM
Ready to pull all my hair out! linuxwise Linux - Laptop and Netbook 12 08-13-2007 12:14 AM
I'm going to pull my hair out! WayneS Linux - Software 1 09-07-2006 08:26 AM
Samba and CUPS - Pull hair out here rjkfsm Linux - General 2 04-18-2005 11:58 AM


All times are GMT -5. The time now is 12:47 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration