Why do these 2 methods result in different outcomes?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why do these 2 methods result in different outcomes?
# 1  
Old 08-21-2018
Why do these 2 methods result in different outcomes?

I've been troubleshooting a ksh93 script I wrote today and have narrowed it down to the root cause. but I don't understand why?

so, i've written the following script to demonstrate the problem i found today:

Code:
#!/bin/ksh

method=$1

FILE_LIST=( $(find /someplace -type f -printf "%T@\t%p\n" 2>/dev/null | sort -r | awk '{print $2}') )

print "found ${#FILE_LIST[*]} files."

typeset -li counter=0
for file in ${FILE_LIST[*]}
do
    case $method in
	0)
	    [[ -f $file ]] \
	        && (( counter++ )) \
	        && continue
	    ;;
	1)
	    if [[ -f "$file" ]] ; then
		(( counter++ ))
		continue
	    fi
	    ;;
	*)
	    print "don't know that method"
	    exit 1
	    ;;
    esac

    print "should never get here: $file"
done

print "counter=$counter"

the code for when method=0 or 1 provides different results. I *thought* it would be the same, and I don't understand why it isn't:

Code:
$ ./t60.ksh 1
found 106 files.
counter=106
$ ./t60.ksh 0
found 106 files.
should never get here: /someplace/.t60.ksh.swp
counter=106

The ".t60.ksh.swp" file is simply the 1st element of the FILE_LIST array. Why does method=0 result in execution of the "should never get here" line once? (and why only once?) I was expecting it to have the same result as method=1.
# 2  
Old 08-22-2018
After running ((counter++)), the exit code is 1, not zero, and hence the continue statement is not executed. The reason is that the value of counter++ is what is stored in counter BEFORE it is incremented, i.e zero. If the value of the expression is zero, the exit code of the ((....)) command is 1. If the value of the expression is not zero, the exit code of the ((....)) command is 0.
# 3  
Old 08-22-2018
The AND operator && in the form of command1 && command2 says that command2 is executed only if command1 returns an exit status of zero. (( counter++ )) will not return zero so continue is not hit.
With method 1 the continue will be executed unconditionally.
# 4  
Old 08-22-2018
To complement rovf correct explanation ..
To have expected result, altho a lot of redundant code is there, increment the variable BEFORE
++counter, instead of AFTER counter++

Or just leave out the ((...)) syntax and use counter=$((counter+1)), which is to be used if you do not require behaviour ((...)) offers.

Regards
Peasant.
# 5  
Old 08-22-2018
thank you everyone. i got it now. i was confused about the return behavior of (( counter++ )). so, to get what i was expecting, i can use either:

Code:
(( ++counter ))
(( counter = counter + 1 ))
counter=$((counter+1))
let counter=counter+1

all of the above would return 0. where as:

Code:
(( counter++ ))

would return 1 (when counter=0), but return 0 after counter > 0.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-22-2018 at 05:24 AM.. Reason: Added CODE tags.
# 6  
Old 08-22-2018
This behaviour is explained in the following URL/book
Numeric Variables and Arithmetic (Learning the Korn Shell, 2nd Edition)

Under 6.2.2. Arithmetic Conditionals

It's a good read if you wish to maintain and program in ksh.

Regards
Peasant.
# 7  
Old 08-22-2018
Not quite. All of
Code:
(( ++counter ))
(( counter = counter + 1 ))
let counter=counter+1

return 1 when run with counter=-1.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum, I'm looking to expand the following command: INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server") Currently this command will check if my server is RedHat server using the grep -qi... (6 Replies)
Discussion started by: greavette
6 Replies

2. Linux

Help with PAM Logging methods.

Hi Folks, Would like to understand if there exists any method to write the logs for LDAP authenticated users and Local Users separately in two different files. If not, then do I distinguish whether the user is LDAP or local without looking at passwd. Bye the way, I am trying this weird... (0 Replies)
Discussion started by: awk-admirer
0 Replies

3. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

4. Shell Programming and Scripting

Perl Methods Calling

Hello I am on my way to improve my wonderful Perl skills, I got an issue which I want to share with you all. I have a Perl module which looks like package Cocoa; require Exporter; @ISA = qw(Exporter); my $a=''; my $b=''; my $c=''; sub new { my $this = shift; # Create... (8 Replies)
Discussion started by: adisky123
8 Replies

5. Solaris

Unix learning methods

I have recently completed Solaris 10 System Administration book by Bill Calkins. Now I want to learn more about UNIX. I have tried to research online but there is too much information and I am sort of overwhelmed and don't know where to start. Can anybody give some idea on how to pursue my learning... (3 Replies)
Discussion started by: saudsos
3 Replies

6. UNIX for Advanced & Expert Users

yum provides methods

What is the difference between these yum provides and whatprovides methods? I know provides and whatprovides give the same results, but different methods of */ and \* give different results. Also whether you put */ and \* in front of the string or behind the string give different results. I have... (0 Replies)
Discussion started by: cokedude
0 Replies

7. Shell Programming and Scripting

Methods to SSH (Perl)...

Can anyone break down the different methods of using SSH in perl? I'm currently using Net::SSH::Expect, which allows me to login to a machine and execute multiple commands without having to ssh again. This feature of holding the session works well for me, but it's slow. If I set timeouts to 4... (3 Replies)
Discussion started by: mrwatkin
3 Replies

8. What is on Your Mind?

Predict Future Outcomes in Our Event Prediction Market

Folks love to predict the future, so we have enabled predicting the future for members. So, please enjoy placing your Forum Bits predicting future outcomes in our new Event Prediction Market. Current events you can predict include science, technology, M&A and other global events: Oracle... (0 Replies)
Discussion started by: Neo
0 Replies

9. Shell Programming and Scripting

file transfer Methods

I would like to write shell script to transfer some ASCII files from HPUX server to Linux server or vice-versa. What options I have for file transfer Methods. 1) FTP 2) rcp 3) HTTP 4)Whatelse? (2 Replies)
Discussion started by: Teh Tiack Ein
2 Replies
Login or Register to Ask a Question