Getting Error count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting Error count
# 1  
Old 07-16-2014
Getting Error count

Hi All,

I have the below code. After doing copy and move if there is an error that should be initially captured as 1 and incremented for each error with exiting. Finally if the total error count to be greater than 2 then I need to exit the script.

########################

Code:
cp a/x.csv b/x.csv
if [ $? -ne 0 ]; then
count it as 1

cp a/d.csv b/f.csv

if [ $? -ne 0 ]; then
count it as 2( Incrementing the error count)

mv b/x.csv b/vin.csv
if [ $? -ne 0 ]; then
count it as 3( Incrementing the error count)

mv b/f.csv b/test.csv
if [ $? -ne 0 ]; then
count it as 3( Incrementing the error count)

If total error count is greater than 2 then 
Exit 1

Please let me know how to acheive this.

Thanks.

Last edited by Don Cragun; 07-16-2014 at 09:38 PM.. Reason: Add CODE tags.
# 2  
Old 07-16-2014
Code:
#!/bin/bash

COUNT=0

cp a/x.csv b/x.csv 2>/dev/null
(( $? != 0 )) && (( COUNT++ ))

cp a/d.csv b/f.csv 2>/dev/null
(( $? != 0 )) && (( COUNT++ ))

mv b/x.csv b/vin.csv 2>/dev/null
(( $? != 0 )) && (( COUNT++ ))

mv b/f.csv b/test.csv 2>/dev/null
(( $? != 0 )) && (( COUNT++ ))

(( COUNT > 2 )) && exit 1

exit 0

This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 07-16-2014
What shell are you using? What OS are you using?

Your pseudo-code raise lots of ambiguities. You have ifs and thens, but no elses or fis.

count it as digit is not a valid shell command and doesn't seem to match the comments before your pseudo-code.

If you are using any POSIX conforming shell (such as ksh or bash), you could also try this alternative to what fpmurchpy suggested:
Code:
count=0
cp a/x.csv b/x.csv || count=$((count + 1))
cp a/d.csv b/f.csv || count=$((count + 1))
mv b/x.csv b/vin.csv || count=$((count + 1))
mv b/f.csv b/test.csv || count=$((count + 1))
exit $((count > 2))

As shown by fpmurphy, some of the arithmetic could be further simplified with a 1993 or later version of ksh or a recent version of bash. This code won't hide the diagnostics if some of the commands fail. (The code fpmurphy suggested will hide any diagnostics.)

It seems very strange to me to try to perform either of those mv commands if the corresponding cp command preceding it failed. It also seems very strange to return a 0 exit code if one or two of the cp or mv commands fails. But this code seems to do what you asked for.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

3. Shell Programming and Scripting

Word Count error

I need to read a trigger file whose name can be: ABC!DEF@2014.txt or ABC!DEF@2014,2015.txt and then carry out functions on those inputs. Currently I am doing: YEAREXPORT { FILE= xyz.txt ls ABC* -l > ${FILE} if ; then log_err "Trigger File ABC* does not exist!" fi (4 Replies)
Discussion started by: rajiv_kool
4 Replies

4. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

5. Shell Programming and Scripting

awk count how many IP have received that error

Hi all, I want to write a awk script that counts unique IPs that have received one special error. For example 25-04-2012;192.168.70.31;1254545454545417;500.0;SUCCESS 25-04-2012;192.168.70.32;355666650914;315126423993;;General_ERROR_23 30-04-2012;192.168.70.33;e;null;null;Failure... (4 Replies)
Discussion started by: arrals_vl
4 Replies

6. Shell Programming and Scripting

awk count how many unique IPs have received that error

Hi all, I want to write a awk script that counts unique IPs that have received one special error. For example 25-04-2012;192.168.70.31;1254545454545417;500.0;SUCCESS 25-04-2012;192.168.70.32;355666650914;315126423993;;General_ERROR_23 30-04-2012;192.168.70.33;e;null;null;Failure... (2 Replies)
Discussion started by: arrals_vl
2 Replies

7. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

Ignore error and get ls file count

I am trying to figure out how to run the below variable assignment in a shell script so that it will snuff the "no such file or directory" and just pass the count value. That way I can do a valid compare in the next step. num=`ls /appsrv/tmp/PrjRefData_20090428-*_in.xml | wc -l` ls:... (3 Replies)
Discussion started by: lynchmob
3 Replies

9. Shell Programming and Scripting

let 'count = 1.2 * 100' error

I am trying to test some multiply function under bash shell, as you can see from the following $ let 'count = 1.2 * 100' bash: let: count = 1.2 * 100: syntax error in expression (error token is ".2 * 100") does not work, seems bash shell does not like ".2" $ let 'count = 1 * 100' ... (2 Replies)
Discussion started by: fedora
2 Replies

10. HP-UX

Count Exceeded Error on HP-UNIX

Sirs/Madame, On my HP-Unix, I came across an error GRECV-count exceeded and as a result of this, systems got hanged and server came down. Graceful shutdown too was not allowed. Kindly, Can anybody help me out in killing this issue Bhavani.R (0 Replies)
Discussion started by: bhavani2006
0 Replies
Login or Register to Ask a Question