Before Job- subroutine to move a file
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 59
- Joined: Sat Jul 05, 2008 11:32 am
Before Job- subroutine to move a file
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!
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!
Code: Select all
if [ -f file1 ]; then mv file1 file2 ;fi
You are the creator of your destiny - Swami Vivekananda
-
- Participant
- Posts: 59
- Joined: Sat Jul 05, 2008 11:32 am
Thank you! I appreciate all your help! DSXchange rocks!!!anbu wrote:Code: Select all
if [ -f file1 ]; then mv file1 file2 ;fi
-
- Participant
- Posts: 59
- Joined: Sat Jul 05, 2008 11:32 am
Anbu,anbu wrote:Code: Select all
if [ -f file1 ]; then mv file1 file2 ;fi
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!
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
"You can never have too many knives" -- Logan Nine Fingers
Chandrathdsx wrote:Anbu,anbu wrote:Code: Select all
if [ -f file1 ]; then mv file1 file2 ;fi
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
Code: Select all
mv file1 file2 file3 dir 2>/dev/null || echo failed
You are the creator of your destiny - Swami Vivekananda
-
- Participant
- Posts: 59
- Joined: Sat Jul 05, 2008 11:32 am
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
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.Chandrathdsx wrote:So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..
-craig
"You can never have too many knives" -- Logan Nine Fingers
"You can never have too many knives" -- Logan Nine Fingers
-
- Participant
- Posts: 59
- Joined: Sat Jul 05, 2008 11:32 am
Craig,chulett wrote: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.Chandrathdsx wrote:So, I am using following code:
mv #SrcDir#/Filename* #TgtDir# || echo Move Failed
and it is working for me..
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.
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
|| 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
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.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Two ways around I could think ofChandrathdsx 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..
Code: Select all
mv #SrcDir#/Filename* #TgtDir# 2>/dev/null
or
Code: Select all
for file in `ls #SrcDir#/Filename*`
do...