Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-01-2015, 02:23 PM   #1
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_count=`ls|grep ^backup|wc -l`
file_to_delete=`ls|grep ^backup|head -1`
file_to_copy=`ls|grep ^backup`

if [[ ${backup_count} > 2 ]]
echo "`date`: Deleting ${file_to_delete}" >> ${log}
rm -f ${file_to_delete}
echo "`date`: File has been deleted." >> ${log}
echo "`date`: Mounting cifs Mirror..." >> ${log}
mount -t cifs //$ /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
Old 09-01-2015, 02:31 PM   #2
Registered: Oct 2014
Location: Fort Wayne
Distribution: Ubuntu 16.04 LTS
Posts: 616
Blog Entries: 2

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

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

Rep: Reputation: 390Reputation: 390Reputation: 390Reputation: 390
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, 04:59 PM   #5
Registered: Aug 2002
Posts: 15,943

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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, 09:07 PM   #6
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
1. See
if [[ ${backup_count} > 2 ]]
is string comparison, not integer.

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

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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)


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

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

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