Hello Experts,
This script to delete a file is submitted from an SAP system which has 2 servers. When it happens to run on server 1, the delete is successful. When it runs on server 2, the delete always fails. All user accounts and permissions have been adjusted to match on both servers. Is it possible to change the script to pass back the actual return code showing why the delete failed, rather than just giving text for when the return is not zero? I did search the forum, but cannot figure out how to pass back the actual return code. Trying echo $? just passes back blank.
Any information is greatly appreciated. Thank you.
echo "Running:" $0
#############################################################################################################################################################################################
# Check to see if the correct number of parameters are entered.
#############################################################################################################################################################################################
if [ $# -lt 2 ]
then
echo "$0, did not get 2 arguments - ABORTING!"
echo "Please enter the path name in PARAM1 as dir/dir/dir"
echo "and enter the file name in PARAM2 as nameXXX"
exit 01
fi
echo "Path:" $1
echo "File:" $2
sleep 60
path=$1
filen=$2
/bin/rm -f $path/$filen
if [ $? -ne 0 ]
then
echo "The script failed to remove the file"
exit 02
else
echo "The file was successfully deleted"
fi
echo 'End of Script'
You didn't mention the OS running on the servers. If it's available on your OS, try replacing the rm command with the unlink command as it'll generate some output if there is an issue deleting a file:
unlink testfile
unlink: cannot unlink 'testfile': Operation not permitted
you need to redirect the error from /bin/rm -f $path/$filen to a log file ...
also, you should get in the habit of indenting your code blocks to make your scripts easier to debug and remove the extraneous "#" in your comments as well as get in the habit of using the shebang line (i.e., #! /bin/bash ) ....
#! /bin/bash
echo "Running:" $0
# Check to see if the correct number of parameters are entered.
if [ $# -lt 2 ]
then
echo "$0, did not get 2 arguments - ABORTING!"
echo "Please enter the path name in PARAM1 as dir/dir/dir"
echo "and enter the file name in PARAM2 as nameXXX"
exit 1
fi
echo "Path:" $1
echo "File:" $2
sleep 60
path=$1
filen=$2
/bin/rm -f $path/$filen 2> /tmp/errlog
if [ $? -ne 0 ]
then
echo "The script failed to remove the file"
exit 2
else
echo "The file was successfully deleted"
fi
echo 'End of Script'
exit 0
Is there NFS in play here? It is possible that the filesystem is mounted read-only on server2, or indeed if it is a local filesystem, is there a difference in the way it is described in /etc/fstab, /etc/vfstab, /etc/filesystems or wherever your OS (not specified, so that makes it harder ) describes filesystems to mount.
Yes, thank you - this checked out OK, but was something I didn't think to check.
---------- Post updated at 03:07 PM ---------- Previous update was at 03:04 PM ----------
Thank you very much. This code produced the log file - the error is a permissions issue. We think possibly the SAP system needs to be recycled before it recognizes the changes made to the user account, but still working on that.
Try this to report rm 's exit code back to the caller
/bin/rm -f $path/$filen
status=$?
if [ "$status" -ne 0 ]
then
echo "The script failed to remove the file"
exit $status
else
echo "The file was successfully deleted"
fi
echo 'End of Script'