Trapping the return status from FIND command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trapping the return status from FIND command
# 1  
Old 08-25-2015
Trapping the return status from FIND command

I have the following code in a script:

Code:
find . \( ! -name . -prune \) -name "cg*" -exec cp -p {}   "${temp_dir}"   \; 

ret_stat=$?

I think the return status is only captured for the 'find' command and not for the 'cp' command. Is there a way to get the return status for the 'cp' command to see if the copy is successful or not?
# 2  
Old 08-25-2015
That's ambiguous, which cp do you want? It could be running it dozens of separate times.
# 3  
Old 08-25-2015
Perhaps using find just to locate names would let you track it yourself:

Code:
find . \( ! -name . -prune \) -name "cg*" > /tmp/$$
ERROR=0

while IFS="" read -r FILENAME
do
        cp -p "$FILENAME" "${temp_dir}" || ((ERROR++))
done < /tmp/$$
rm -f /tmp/$$

echo "$ERROR files failed to copy."

# 4  
Old 08-25-2015
The way I am looking at it is even if any one of the copy failed, I want to get notified byt an alert. Is that possible??
# 5  
Old 08-25-2015
How about the code I showed you, plus checking the value of ERROR to see whether an alert should be sent?
# 6  
Old 08-25-2015
Your find prunes sub-directories and does not filter for anything - why not
Code:
cp -p cg* "${temp_dir}"

?
# 7  
Old 08-26-2015
Thank you. it works. But, I am trying to test it by hard coding a file in /tmp/filelist (replaced $$ with filelist) that doesn't exist. When I run the script at that time it gives an error like this:

Code:
 ERROR++: more tokens expected

Idea is to trap the failures while copying and then send an alert. Any clue why that is happening.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find command on a empty directory - bad status

when I run the following command in AIX (bash), find ./* I get the following error. find: bad status-- ./* Thats becasuse, its an empty directory. The same works, when there the directory is not empty. Even though the find deesnt have to rerun any result. My full find command would look... (4 Replies)
Discussion started by: deepakwins
4 Replies

2. UNIX for Dummies Questions & Answers

Exit Status Of Find Command

Hello All, I am trying to capture the exit status of find command and want to delete the files only when it is successful. But it is always returning me as success even if the pattern of that file doesn't exist in the current directory. please help, checked manual page but couldn't able to figure... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

Unable to find exit status of piped command

Lois_Answer_Code=`sipsak -vv -s sip:192.168.1.3|grep -A 1 "reply received after"|grep SIP|awk '{print $2}'`How to find the exit status of | (12 Replies)
Discussion started by: proactiveaditya
12 Replies

4. UNIX for Dummies Questions & Answers

Find command returning bad status--

would like to remove the post (8 Replies)
Discussion started by: vk39221
8 Replies

5. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

6. Shell Programming and Scripting

Trapping program return code

Hello I have a program (prog) that accepts a parameter in order to execute some internal loop grabbing memory in each iteration. I'm using top to monitor the memory usage and to produce an output. Thus I need the program's pid as a parameter to top. I capture pid using myPID=$!. I'm also... (5 Replies)
Discussion started by: pavlosgr
5 Replies

7. Shell Programming and Scripting

Find command return type

Hi all does find command return anything if the file to be searched is not found? Like if I search from a file in a dir does it return false or null if the file is not found? Please suggests. (3 Replies)
Discussion started by: Veenak15
3 Replies

8. Shell Programming and Scripting

command does not return exit status due to tee

Hi, I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command. gmake all 2>&1 | tee log But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee. # false 2>&1 | tee Log # echo $? 0... (2 Replies)
Discussion started by: anand_bh
2 Replies

9. UNIX for Dummies Questions & Answers

how to find the exit status for the last executed command

I am executing a find command in my script i.e find $2 -type f -name '*.gif' -mtime +$1 -exec rm {} \; how do i check that this command is executed properly.. i would lke t trap the errror and display my error message kinly help.. this is an urgent issue. (1 Reply)
Discussion started by: vijay.amirthraj
1 Replies

10. Programming

How to find the exit status of last command in Unix?

Hi, I want to find the exit status of the last executed command in C Shell. Tried $? but getting the error Variable syntax...$? does not seem to work in C shell.. is there any other command in C shell to find the exit status of last command? Thanks in advance, raju (1 Reply)
Discussion started by: rajugp1
1 Replies
Login or Register to Ask a Question