I use bash scripts in Linux to Migrate Database output files and I was wondering of a way to handle errors when executing a Linux command in a bash script.
For example normally when I want to loop through files in a directory I will write it like this
# list files and grep results for .sql extension
for FILE in `ls | grep ".sql"`
do
echo "found file: $FILE"
done
Which works perfectly because grep returns the file name if it has a .sql extension or returns nothing
I was wondering how to use a Linux command that returns a result or an error such as
ls ./*.sql
which returns the name of the file but if it doesn't find a file it returns the error
ls: ./*.sql: No such file or directory
Is it possible to check if a Linux command is returning an error in a bash script?