Before Job- subroutine to move a file

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
Chandrathdsx
Participant
Posts: 59
Joined: Sat Jul 05, 2008 11:32 am

Before Job- subroutine to move a file

Post 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!
anbu
Premium Member
Premium Member
Posts: 596
Joined: Sat Feb 18, 2006 2:25 am
Location: india

Post by anbu »

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi
You are the creator of your destiny - Swami Vivekananda
Chandrathdsx
Participant
Posts: 59
Joined: Sat Jul 05, 2008 11:32 am

Post by Chandrathdsx »

anbu wrote:

Code: Select all

if [ -f file1 ]; then mv file1 file2 ;fi
Thank you! I appreciate all your help! DSXchange rocks!!!
Chandrathdsx
Participant
Posts: 59
Joined: Sat Jul 05, 2008 11:32 am

Post 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!
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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`
-craig

"You can never have too many knives" -- Logan Nine Fingers
anbu
Premium Member
Premium Member
Posts: 596
Joined: Sat Feb 18, 2006 2:25 am
Location: india

Post 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
You are the creator of your destiny - Swami Vivekananda
Chandrathdsx
Participant
Posts: 59
Joined: Sat Jul 05, 2008 11:32 am

Post 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..
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Please mark this thread as Resolved using the green button at the top.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post 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.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Chandrathdsx
Participant
Posts: 59
Joined: Sat Jul 05, 2008 11:32 am

Post 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.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post 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
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Kryt0n
Participant
Posts: 584
Joined: Wed Jun 22, 2005 7:28 pm

Post 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
Post Reply