INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

variable and +=1

variable and +=1

(OP)


hello.

why the counter in the example is not increasing???

CODE

$ cat testscpt.ksg
A1=$1
A2=$2
ERRCOUNT=0
[[ ! -z $A1 ]] && echo V1 :: $A1 || ( echo "V1 :: CHECK IT!!!" ; ((ERRCOUNT+=1)) )
echo after A1 ERRCOUNT = $ERRCOUNT
[[ ! -z $A2 ]] && echo V2 :: $A2 || ( echo "V2 :: CHECK IT!!!" ; let ERRCOUNT+=1 )
echo after A2 ERRCOUNT = $ERRCOUNT
$
$
$
$
$
$ ./testscpt.ksg
V1 :: CHECK IT!!!
after A1 ERRCOUNT = 0
V2 :: CHECK IT!!!
after A2 ERRCOUNT = 0
$
$
$ ./testscpt.ksg 1
V1 :: 1
after A1 ERRCOUNT = 0
V2 :: CHECK IT!!!
after A2 ERRCOUNT = 0
$
$ ./testscpt.ksg 1 2
V1 :: 1
after A1 ERRCOUNT = 0
V2 :: 2
after A2 ERRCOUNT = 0
$ 

RE: variable and +=1

Hi

It is incremented, but as it runs in a child process, your script has no way to reach that value.

Compare the following two and your code :

CODE

master # var='initial'; ( var='changed' ); echo "$var"
initial

master # var='initial'; { var='changed'; }; echo "$var"
changed 

Feherke.
http://feherke.github.com/

RE: variable and +=1

(OP)

thank you.
is the code below ok now - don't I need to write "classic" if...then...else...fi lopps?

doing a few tests it seems to do jobs correctly...

can you explain why the "((ERRCOUNT+=1))" doesn't need ";" at the end as opposed to "let ERRCOUNT+=1"?

CODE

$ cat ./testscpt.ksh
A1=$1
A2=$2
A3=$3
A4=$4
A5=$5
A6=$6
ERRCOUNT=0
[[ ! -z $A1 ]] && echo V1 :: $A1 || { echo "V1 :: CHECK IT!!!" ; ((ERRCOUNT+=1))}
echo after A1 ERRCOUNT = $ERRCOUNT
[[ ! -z $A2 ]] && echo V2 :: $A2 || { echo "V2 :: CHECK IT!!!" ; let ERRCOUNT+=1 ; }
echo after A2 ERRCOUNT = $ERRCOUNT
[[ ! -z $A3 ]] && echo V3 :: $A3 || { echo "V3 :: CHECK IT!!!" ; ((ERRCOUNT+=1)) ; }
echo after A3 ERRCOUNT = $ERRCOUNT
[[ ! -z $A4 ]] && echo V4 :: $A4 || { echo "V4 :: CHECK IT!!!" ; let ERRCOUNT+=1 ; }
echo after A4 ERRCOUNT = $ERRCOUNT
[[ ! -z $A5 ]] && echo V5 :: $A5 || { echo "V5 :: CHECK IT!!!" ; ((ERRCOUNT+=1))}
echo after A5 ERRCOUNT = $ERRCOUNT
[[ ! -z $A6 ]] && echo V6 :: $A6 || { echo "V6 :: CHECK IT!!!" ; ((ERRCOUNT+=1))}
echo after A6 ERRCOUNT = $ERRCOUNT
$ ./testscpt.ksh 1 2
V1 :: 1
after A1 ERRCOUNT = 0
V2 :: 2
after A2 ERRCOUNT = 0
V3 :: CHECK IT!!!
after A3 ERRCOUNT = 1
V4 :: CHECK IT!!!
after A4 ERRCOUNT = 2
V5 :: CHECK IT!!!
after A5 ERRCOUNT = 3
V6 :: CHECK IT!!!
after A6 ERRCOUNT = 4
$ ./testscpt.ksh 1 2 2 3 4
V1 :: 1
after A1 ERRCOUNT = 0
V2 :: 2
after A2 ERRCOUNT = 0
V3 :: 2
after A3 ERRCOUNT = 0
V4 :: 3
after A4 ERRCOUNT = 0
V5 :: 4
after A5 ERRCOUNT = 0
V6 :: CHECK IT!!!
after A6 ERRCOUNT = 1
$ ./testscpt.ksh 1 2 2 3 4 6
V1 :: 1
after A1 ERRCOUNT = 0
V2 :: 2
after A2 ERRCOUNT = 0
V3 :: 2
after A3 ERRCOUNT = 0
V4 :: 3
after A4 ERRCOUNT = 0
V5 :: 4
after A5 ERRCOUNT = 0
V6 :: 6
after A6 ERRCOUNT = 0
$ 

RE: variable and +=1

Hi

Quote (w5000)

is the code below ok now - don't I need to write "classic" if...then...else...fi lopps?
Mostly Ok. Whether you use the if shell keyword or || list operator hardly depends on your habits. Personally I prefer list operators for simple cases. However the need for {} group command usually means not simple case.

Quote (w5000)

can you explain why the "((ERRCOUNT+=1))" doesn't need ";" at the end as opposed to "let ERRCOUNT+=1"?
No idea. Bash throws syntax error, Dash misinterprets the command and expects continuation and MKsh accepts and executes it correctly. So I always add the semicolon, just to be sure.

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close