Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable and +=1 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
0
0
PL


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
$
 
Don't use a subshell ...
Code:
...
if [[ ! -z $A1 ]]; then
  echo V1 :: $A1
else
  echo "V1 :: CHECK IT!!!"; ((ERRCOUNT+=1))
fi
echo after A1 ERRCOUNT = $ERRCOUNT
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
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:
[blue]master #[/blue] var='initial'; ( var='changed' ); echo "$var"
initial

[blue]master #[/blue] var='initial'; { var='changed'; }; echo "$var"
changed

Feherke.
[link feherke.github.com/][/url]
 

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!!!" ; [bold]((ERRCOUNT+=1))[/bold]}
echo after A1 ERRCOUNT = $ERRCOUNT
[[ ! -z $A2 ]] && echo V2 :: $A2 || { echo "V2 :: CHECK IT!!!" ; [bold]let ERRCOUNT+=1 ;[/bold] }
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
$ [bold]./testscpt.ksh 1 2[/bold]
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
$ [bold]./testscpt.ksh 1 2 2 3 4[/bold]
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
$ [bold]./testscpt.ksh 1 2 2 3 4 6[/bold]
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
$
 
Hi

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

w5000 said:
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.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top