Visit Our UNIX and Linux User Community


Pass back return code for file delete failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass back return code for file delete failure
# 1  
Old 08-08-2013
Pass back return code for file delete failure

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.

Code:
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'
 

# 2  
Old 08-08-2013
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:

Code:
unlink testfile
unlink: cannot unlink 'testfile': Operation not permitted

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 08-08-2013
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) ....
Code:
#! /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

This User Gave Thanks to Just Ice For This Post:
# 4  
Old 08-08-2013
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 Smilie) describes filesystems to mount.


Does this give you something to work with?




Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 08-08-2013
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.
# 6  
Old 08-08-2013
Quote:
Originally Posted by SAPDEVEL
. . . Is it possible to change the script to pass back the actual return code showing why the delete failed, . . .
Try this to report rm's exit code back to the caller

Code:
/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'


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help to delete carriage return and new line in csv file

Hi All, I have a problem loading the data from a csv file As you see below in the Input ,For the Data starting with " there are 2 lines, which i want to make them into single without changing the format of that data. You can see the desired output below: While i try to open the csv file and... (4 Replies)
Discussion started by: mlavanya
4 Replies

2. Shell Programming and Scripting

Need to pass Oracle SQL output to Linux and back... Help!

Hi all, Hopefully you can help. This is what I'm trying to achieve: Obtain a list of usernames out of an Oracle Database Based on this list, link each username with an Oracle Internet Directory (OID) GUID Using the username and GUID perform a database update for all users Here are the... (7 Replies)
Discussion started by: exm
7 Replies

3. UNIX for Dummies Questions & Answers

Pass value back to unix variable

i had this unix korn shell code that connects to oracle database and execute the oracle procedure. i need to add a variable that indicates the oracle procedure failed. basically the variable is to check if the oracle procedure failed it will assign 1 and when the variable is equal to 1 it will not... (4 Replies)
Discussion started by: wtolentino
4 Replies

4. Programming

How to pass int and return string in C?

hi I want to write a function which takes int as input and returns a string like this. char GetString(int iNo) { switch(iNo) { case 0: return "Zero"; break; case 1: return "One"; break; } } void main() { int i; printf("Enter... (1 Reply)
Discussion started by: atharalikhan
1 Replies

5. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

6. Shell Programming and Scripting

Want to trap script error and return line number of failure

Hey all UNIX nerds- I've built a shell script which runs pretty well- only I want it to have much better error trapping. (Like the kind I could apply to every shell script I write). I'm not a UNIX genius, and could really use a bit of help. The original script goes something like this: 1... (3 Replies)
Discussion started by: stevekerver
3 Replies

7. Shell Programming and Scripting

Need to return fail or pass from shell script on the basis of pl/sql code execution

Hi guys, I am quite new in shell scripting. I am tring to promote some oracle jobs into control-M. In control-M, I am calling a script which establishes a connection with database and execute some procedures. Now I want if that PL/sql Block got failed script should return failure to... (2 Replies)
Discussion started by: alok1301
2 Replies

8. Shell Programming and Scripting

Is it possible to pass variable from awk to shell back

I am passing a varaible to from Shell to awk then I am doing some maniplation for that variable inside awk. I want that maniplated variable value back to shell , Is this possible .Please let me know. (12 Replies)
Discussion started by: unishiva
12 Replies

9. UNIX for Dummies Questions & Answers

How to pass a oracle variable back to the shell script

Hi, I am calling an oracle function that returns a number (either 0 or 2), how do I pass that pass to the wrapping shell script as I would like to do other things based on the value returned by the oracle function. Your help will be appreciated. -------------------------- sqlplus / <<... (3 Replies)
Discussion started by: Jtrinh
3 Replies

10. Shell Programming and Scripting

pass parameter back to calling program

Hi all, I am running AIX version 4. I have a shell script that is calling another script. I want the called script to obtain a value and pass it back to the calling script. So far, I know that to pass a parameter to a called script is as such: sh proc2.sh $1 $2 etc. What I don't know how... (11 Replies)
Discussion started by: jthomas
11 Replies

Featured Tech Videos