Exit always returns 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit always returns 0
# 1  
Old 08-11-2018
Exit always returns 0

This returns 0 even when it does not delete any files.

Is it because -print returns 0?

Code:
RETVAL=$?
Docs_Backups=/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/
Scripts_Backups=/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/

# create some old files
#touch -d 20120101 /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/test1
#touch -d 20120101 /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/test2
#touch -d 20120101 /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/test3
echo "Files older than 3 days."
find $Docs_Backups -mindepth 1 -mtime +3 -print -delete 
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
find $Scripts_Backups -mindepth 1 -mtime +3 -print -delete 
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "Files older than 3 days have been deleted."

# 2  
Old 08-11-2018
I can't find an exit in your code snippet. Assuming you run through the entire code, and leave it with the last line shown, it's obvious it returns 0 as the last echo succeeds.

If you ask yourself why your finds always output a success, look at the RETVAL variable: it's set by the exit code of an obscure command run before the entry into your code snippet.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-11-2018
find simply doesn't return nonzero unless something prevents it from doing what you asked it to do. If it tries to do open a directory and the system tells it "Permission denied", that's an error for example. "Zero files" on the other hand is not an error, just a fact.

Anyway your use of $RETVAL is wrong, because nothing ever changes it. It remains the same throughout your entire program. Using $? instead would also be wrong, because everything changes it -- every time you run any program or test any condition, that changes the value of $?. Even [ $? -eq 0 ] changes the value of $? afterwards. If you want to do more complicated logic in your shell script, use proper if/then statements, case statements, and the like.

What I might try is something like this:

Code:
# Always quote your variables
find "${Docs_Backups}" -mindepth 1 -mtime +3 -print -delete > "/tmp/$$"

if [ -s "/tmp/$$" ]
then
        echo "Found some files"
else
        echo "Found zero files"
fi

rm -f "/tmp/$$"

/tmp/$$ is a simple way of making a random temporary file, since $$ is your process ID and ought to be unique while your program is running.

-s is true when the file has any contents, false when it has no contents.

Last edited by Corona688; 08-11-2018 at 01:48 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 08-11-2018
Thanks for the valuable info.

------ Post updated at 06:50 PM ------

I made a change in my user profile and now I don't like the new look.


I would like to attach a screenshot, but could not find a way.



Thanks.
# 5  
Old 08-12-2018
Quote:
Originally Posted by drew77
Thanks for the valuable info.

I would like to attach a screenshot, but could not find a way.

Thanks.
Please read the FAQ for Forum Usage - Includes Attachments:

You can also attach images by uploading images to your Album.

and the cut and paste the provide IMG tags in the post.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in function returns value

Hi, I need to return a value from the function. the value will be the output from cat command which uses random fucntion. #!/bin/ksh hello() { var1=$(`cat /dev/urandom| tr -dc 'a-zA-Z0-9-!%&()*+,-/:;<=>?_'|fold -w 10 | head -n 1`) echo "value is" var1 return var1 } hello var=$?... (2 Replies)
Discussion started by: Nandy
2 Replies

2. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies

3. UNIX for Dummies Questions & Answers

Help with Functions and Value returns

mon_yy=${1} date_found=`find_end_day $mon_yy` export_dealer_changes ${date_found} Hello I am trying to pull a formatted date back from the function find_end_day and pass it into the function export_dealer_changes. When I try the above the variable date_found is empty. I have tried various... (3 Replies)
Discussion started by: treemyf
3 Replies

4. Shell Programming and Scripting

Calculation returns no value

#/bin/sh ..... #convert memory to MB let "mmsize_a= ($mmsize)/256" let "mminuse_a= ($mminuse)/256" let "mmfree_a= ($mmsize_a -$mminuse_a)" let "mmfreepercent= (($mmfree_a)/($mmsize_a))*100" # #format output echo "\n\n######################" >>$sndFile echo "\n$sysName Total Memory usage"... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

5. Shell Programming and Scripting

Grep returns nothing

Hi all, I am trying to grep a .txt file for a word. When I hit enter, it returns back to $ The file is 4155402 in size and is named in this way: *_eveningtimes_done_log.txt I use this command, being in the same directory as the file: grep -i "invalid" *_eveningtimes_done_log.txt ... (16 Replies)
Discussion started by: DallasT
16 Replies

6. UNIX for Dummies Questions & Answers

Grep without returns...

Is there a command where I can pipe my grep into it and it will output it with spaces rather than returns? Example I want to turn prompt$ grep blah file blah blah into this prompt$ grep blah file | someCommand blah blah (1 Reply)
Discussion started by: mrwatkin
1 Replies

7. UNIX for Dummies Questions & Answers

what is meaning of exit(0) and exit(1)

can u tell me what is the meaning of exit(0),exit(1),exit(2) what is diff amonng these. Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

8. Programming

exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use? (9 Replies)
Discussion started by: enuenu
9 Replies

9. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question