BASH Script, stops on an error, is there a way to make it continue automatically?

So I have a script that renames a bunch of files:

#!!/bin/bash
mv file1old.jpg file1new.jpg
mv file2old.jpg file2new.jpg
mv file3old.jpg file3new.jpg


The problem is: let's just say file2old.jpg doesn't exist, well it makes an error in my script and file3old.jpg never gets processed.  How can I avoid the script breaking down like this?

www_puertoricoautoforo_comAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Hi,

add

trap "" ERR

at the top of your script. That should do the trick.

wmp
0
martin_2110Commented:
Strange when i run it, it continues on and tries to move all the files when I run it. few things to try.
1. there is 2 !! in your first line there should only be one.
2. use full paths /bin/mv not mv
3. that does not work you could test if the file is there before running the move I will attach a script.



#!!/bin/bash
if [ -f file1old.jpg ]
then
mv file1old.jpg file1new.jpg
fi

if [ -f file2old.jpg ]
then
mv file2old.jpg file2new.jpg
fi

if [ -f file2old.jpg ]
then
mv file3old.jpg file3new.jpg
fi

Open in new window

0
www_puertoricoautoforo_comAuthor Commented:
is there an easier , 1 line way to make it run like a rename.bat file in DOS?

when I do this, it just works, is there a similar way for linux?

ren file1old.jpg file1new.jpg
ren file2old.jpg file2new.jpg
ren file3old.jpg file3new.jpg
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

martin_2110Commented:
how do you execute the script, You might try these ways.
./script.sh
. script.sh
bash script.sh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TobiasHolmCommented:
Hi!

The attached script works. It shows an error, just like in DOS, but it continues to process the script.

Regards, Tobias


#!/bin/bash
mv file1 file1.old
mv file9 file2.old
mv file3 file3.old

Open in new window

1mvExample.gif
0
martin_2110Commented:
you should try adding the: trap "" ERR
That someone recommended.
0
TobiasHolmCommented:
The 'trap' command doesn't prevent the error message from showing or help the script to continue processing.

To get rid of the error output, use 2>/dev/null
1mvWithoutErr.gif
0
www_puertoricoautoforo_comAuthor Commented:
i see thanks for the help, i think i was getting errors because i wasn't using quotes on a certain file name with parenthesis in the file name.


thanks for all the help everyone.
0
TintinCommented:
No need to script this when you have the rename command

rename 's/old/new/' file?old.jpg
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.