LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pull my hair out (https://www.linuxquestions.org/questions/linux-newbie-8/pull-my-hair-out-4175552347/)

Archangelmscj 09-01-2015 02:23 PM

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

Ihatewindows522 09-01-2015 02:31 PM

Are you running the script as root?

Archangelmscj 09-01-2015 02:36 PM

@Ihatewindows522
Yes I am running the script as root

descendant_command 09-01-2015 03:59 PM

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

michaelk 09-01-2015 04:59 PM

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.

chrism01 09-01-2015 09:07 PM

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.

Shadow_7 09-02-2015 12:48 PM

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)


All times are GMT -5. The time now is 11:16 PM.