How to Collect Logs from My Script (Maybe Using File Descriptors)

bash

#1

Below is a part of my script . Whenever the script executes ‘ls -ld $dir1’ it throws a msg while script is in execution. Could someone let me know to avoid that message and could be collected separately via stderr or logs by using any of the filedescriptor.

echo -e "\nChecking if the directory $dir1 already exists\n"                                                     

ls -ld $dir1  

  

if [ $? -eq 0 ]                                                                                                  

then                                                                                                             

echo -e "\nDirectory exists!!! Checking to verify if the same directory exists in /etc/fstab\n"                  

else                                                                                                             

echo -e "\nCreating directory $dir1\n"; mkdir $dir1                                                              

echo -e "$dir1\t$ip1($per1,sync,$rperm2,no_subtree_check)" >> share                                              

fi                                                                                                               

done

#2

simply don’t use ls inside scripts!

if test -d "$dir1"
then
   :...
else
   :...
fi