• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

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?

0
www_puertoricoautoforo_com
Asked:
www_puertoricoautoforo_com
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
martin_2110Commented:
how do you execute the script, You might try these ways.
./script.sh
. script.sh
bash script.sh
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now