Page 1 of 1

Before Job- subroutine to move a file

Posted: Mon Feb 22, 2010 1:25 pm
by Chandrathdsx
I am using a before job subroutine to move a file. But, if file does not exist I do not want to abort my job.
Can anyone suggest me how to do this?
Before-job subrotine (ExecSH), input Value : mv file1 file2
I am looking for any command that I can use in input value to move the file only if it exists so that my job does not abort even if the file does not exist.

Thanks!

Posted: Mon Feb 22, 2010 1:31 pm
by anbu

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi

Posted: Mon Feb 22, 2010 1:49 pm
by Chandrathdsx
anbu wrote:

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi
Thank you! I appreciate all your help! DSXchange rocks!!!

Posted: Mon Feb 22, 2010 2:25 pm
by Chandrathdsx
anbu wrote:

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi
Anbu,

One question, if I have multiple files to handle, how am I going to do this?
If I have multiple files, I want to move them all to a different directory in before routine. But, I do not want to abort my job if no files exist.

Thanks!

Posted: Mon Feb 22, 2010 3:43 pm
by chulett
I would probably script something rather than trying to do it all in "one line" but look into "ls | wc -l" with a result of > 0. You'll also need to redirect STDERR to suppress the "no files found" message that comes with the zero.

Posted: Mon Feb 22, 2010 3:54 pm
by chulett
Or something like so to set a variable you can test and conditionally act upon:

Code: Select all

fCount=`ls <pattern> 2>&1 |grep -v 'not found' |wc -l`

Posted: Mon Feb 22, 2010 4:28 pm
by anbu
Chandrathdsx wrote:
anbu wrote:

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi
Anbu,

One question, if I have multiple files to handle, how am I going to do this?
If I have multiple files, I want to move them all to a different directory in before routine. But, I do not want to abort my job if no files exist.

Thanks!

Code: Select all

for file in file1 file2 file3
do
   if [ -f file1 ]; then mv file1 dir ;fi
done
The below code will execute succesfully even one or all the files are missing. But it will run successfully even if directory is missing.

Code: Select all

mv file1 file2 file3 dir 2>/dev/null || echo failed

Posted: Tue Feb 23, 2010 9:21 pm
by Chandrathdsx
Thank you for all your replies.

I do not know my exact file names as these are suffixed with timestamp.
So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..

Thanks again..

Posted: Tue Feb 23, 2010 10:46 pm
by ray.wurlod
Please mark this thread as Resolved using the green button at the top.

Posted: Wed Feb 24, 2010 7:57 am
by chulett
Chandrathdsx wrote:So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..
I'm always curious in situations like this - did you look up what the "pipe pipe" means in UNIX or since it works for you are you just using it? Serious question, btw. It has a sister, "&&", that can also be helpful when stringing multiple commands together.

Posted: Thu Feb 25, 2010 1:39 pm
by Chandrathdsx
chulett wrote:
Chandrathdsx wrote:So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..
I'm always curious in situations like this - did you look up what the "pipe pipe" means in UNIX or since it works for you are you just using it? Serious question, btw. It has a sister, "&&", that can also be helpful when stringing multiple commands together.
Craig,
Thanks for the question! I did refer to some unix manuals before I adopted it. The 2nd command, (i.e., command after '||' (double pipe)) executes only if no file exists to move. '&&' is kind of opposite of '||'. Please let me know if my understanding is not correct.

Thanks.

Posted: Thu Feb 25, 2010 4:01 pm
by ray.wurlod
|| and && are called pipeline operators - they exist between two commands in a pipeline.

command1 || command2
command2 will only execute if command1 fails (its exit status is non-zero)

command1 && command2
command2 will only execute if command1 succeeds (its exit status is zero)

command1 ; command2
command2 will execute unconditionally after command1 completes

Posted: Thu Feb 25, 2010 4:43 pm
by Kryt0n
Chandrathdsx wrote:Thank you for all your replies.

I do not know my exact file names as these are suffixed with timestamp.
So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..

Thanks again..
Two ways around I could think of

Code: Select all

 mv #SrcDir#/Filename* #TgtDir# 2>/dev/null 
which effectively suppresses error messages

or

Code: Select all

 for file in `ls #SrcDir#/Filename*`
do... 
completing as per previous suggestion... although don't quite need the if statement since you already know the files exist