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?
# 8  
Old 08-22-2018
But the standard way of increment without evaluating will work as you expect always.
Code:
+ counter=-1
+ typeset -li counter
counter=$(( counter + 1 ))
+ counter=0
echo $counter $?
+ echo 0 0
0 0

# 9  
Old 08-22-2018
thanks people for helping me understand the subtleties. I think for the sake of clarity, I'm just going to use something like:

Code:
if [[ -f "$file" ]] ; then
	(( counter++ ))
	continue
fi

# 10  
Old 08-23-2018
For the sake of clarity, *I* would write it like this:

Code:
if [[ -f "$file" ]]
then
  (( counter++ ))
elif [[ -e $file ]]
then
  echo "$file is not a plain file, but $(file $file)"
else
  echo "$file disappeared"
fi

This message is probably clearer than "should never get here" ;-)
# 11  
Old 08-27-2018
Quote:
Originally Posted by BLinux
thanks people for helping me understand the subtleties. I think for the sake of clarity, I'm just going to use something like:

Code:
if [[ -f "$file" ]] ; then
	(( counter++ ))
	continue
fi

For the sake of clarity i suggest you stay away from keywords like "continue" and "break" as long as possible and use them only as a very last resort. You see, both are basically variants of the GOTO and we know how we feel about GOTO, don't we?

In your example code i don't even understand what the "continue" should accomplish: the case-part already finishes at this point and the next iteration of the for-loop will start anyway, so the difference is perhaps the time it takes to execute the "fi", the ";;" and the "next" - is it only me or should that really be negligible?

I hope this helps.

bakunin
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