LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2011, 01:35 AM   #1
w6lsn
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Command line works, won't work from inside bash script


writing script to create backup of file by adding datetime to file name. Basically test for file presence
if there, cp with datetime
then rm original

cp works fine from command line but get
cannot stat `full path to file': No such file or directory

Code:
#!/bin/bash
base_dir='~/html/'
for a in CVP_dadamail; do
   ldir='/.dada_files/.logs/'
   f_name='errors.txt'
   f2bu=$base_dir$a$ldir$f_name
   dt1="$(date +%Y%m%d%H%M%S)"
   backup="$base_dir$a$ldir$dt1.$f_name"
   if [ -f $f2bu ]; then 
      cp $f2bu $backup
      rm $f2bu
   fi
done
here are the errors:
cp: cannot stat `~/html/CVP_dadamail/.dada_files/.logs/errors.txt': No such file or directory

rm: cannot remove `...': No such file or directory

The for statement is a placeholder as I have same file to backup out of several directories.

using "bash -x scriptname" -OR- inserting echos, I can see I've constructed the strings properly. Believing it might be related to the hidden directories, I tried setting the shopt "glob" options to no avail.

Ultimately I'll add the other directories to the for loop and then run this from a cron job, so if you see potential pitfalls knowing I'm headed in that direction...

believe construct would be
Code:
for a in dir1 dir2 dir3; do
Thanks for looking...
 
Old 02-16-2011, 01:38 AM   #2
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
Quote:
Originally Posted by w6lsn View Post
writing script to create backup of file by adding datetime to file name. Basically test for file presence
if there, cp with datetime
then rm original

cp works fine from command line but get
cannot stat `full path to file': No such file or directory

Code:
#!/bin/bash
base_dir='~/html/'
for a in CVP_dadamail; do
   ldir='/.dada_files/.logs/'
   f_name='errors.txt'
   f2bu=$base_dir$a$ldir$f_name
   dt1="$(date +%Y%m%d%H%M%S)"
   backup="$base_dir$a$ldir$dt1.$f_name"
   if [ -f $f2bu ]; then 
      cp $f2bu $backup
      rm $f2bu
   fi
done
here are the errors:
cp: cannot stat `~/html/CVP_dadamail/.dada_files/.logs/errors.txt': No such file or directory

rm: cannot remove `...': No such file or directory

The for statement is a placeholder as I have same file to backup out of several directories.

using "bash -x scriptname" -OR- inserting echos, I can see I've constructed the strings properly. Believing it might be related to the hidden directories, I tried setting the shopt "glob" options to no avail.

Ultimately I'll add the other directories to the for loop and then run this from a cron job, so if you see potential pitfalls knowing I'm headed in that direction...

believe construct would be
Code:
for a in dir1 dir2 dir3; do
Thanks for looking...
Hi,
Instead of using `~/html/CVP_dadamail/.dada_files/.logs/errors.txt, use the absolute path for that file because this path is not accessible by your script.
 
1 members found this post helpful.
Old 02-16-2011, 02:25 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
for a in CVP_dadamail; do
Even if adding several directories to this for loop, it is a very inefficient way to perform this task.
As you have pretty much written every piece of the path by hand, just placed in variables, you may as well have just done the cp by hand anyway.

Maybe if you explained your goal it would be easier to assist?

As Sayan has said above, unless the script is always to be run as the user who has the directories it will not work for anyone else when using tilde (~).
 
1 members found this post helpful.
Old 02-16-2011, 09:56 AM   #4
w6lsn
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
changing the ~ to absolute, works. Thanks!
Already added second loop to rotate through the files in each directory.


Can anyone suggest a good intermediate book on Linux / bash or is there a place on this board to look?
I prefer a book with solid examples of code that are explained rather than the book teaching me looping or conditional branching etc.
 
Old 02-17-2011, 05:45 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by w6lsn View Post
changing the ~ to absolute, works.
You put to ~ in single (or double) quotes. This would be in the variable then as literal text. You could have in the script:

Code:
eval backup="$base_dir$a$ldir$dt1.$f_name"
This would interpret again the already substituted variables.


Quote:
Originally Posted by w6lsn View Post
Can anyone suggest a good intermediate book on Linux / bash or is there a place on this board to look?
I prefer a book with solid examples of code that are explained rather than the book teaching me looping or conditional branching etc.
One book I like and advertise to the local students: Sams Teach Yourself Shell Programming in 24 hrs. After reading the book `man bash | a2ps` and `man awk | a2ps` can be considered.
 
1 members found this post helpful.
  


Reply



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
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
mkdir won't work in a script but works on command line ifeatu Linux - Newbie 3 02-04-2010 02:26 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05: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
Open Source Consulting | Domain Registration