rm error codes and logging

Afternoon ladies and gents,

I am trying to create a simple script to remove a certain file from a user's network profile location. The removal works ok, but in the interest of overkill I would like to add a simple error detection (such as file doesn't exist or permission denied)

Currently, it works perfect assuming nothing is missing and I have permissions to run it. It outputs all the removed files to the log. If permission denied or file does not exist, it skips the append ending and dumps it into terminal. Is there any way I may be able to log the errors as well?

Part in question:

array=( $( ls -1p | grep / | sed 's/^\(.*\)/\1/') )
for (( i=0; i<${#array
[*]}; i++));
    do
        rm -v /profiles/${array}path/to/file.extension >> ~/Desktop/MyLog.log
    done

Note: The $array variable returns "FolderName/" instead of just "FolderName". This explains it being "/profiles/${array[i]}path/to/file.extension" instead of "/profiles/${array[i]}/path/to/file.extension".

I understand it may involve a small "if-then" statement, I am just clueless go about it.

Thank You!

-Hate

---------- Post updated at 02:34 PM ---------- Previous update was at 02:25 PM ----------

Dave_L on Ubuntu Forums solved this (very quickly I might add).

rm -v /profiles/${array}path/to/file.extension >> ~/Desktop/MyLog.log 2>&1

Adding "2>&1" after MyLog.log was the fix.

Thanks!

-Hate

Nine minutes is quick. Congrats to Dave.

Thanks for updating :slight_smile:

You could try;

do
if [[ -f /profiles/${array}path/to/file.extension ]]
               then
               rm -v /profiles/${array}path/to/file.extension >> ~/Desktop/MyLog.log
               else
               print "A Message" >> ~/Desktop/MyLog.log
          fi
done

You had better check the syntax on the if as I'm currently working my way through a nice bottle of 18 Year Old Glenlivett.

Regards

Dave