2

I really can't see what the issue is with my script is. I've considered missing quotations or other syntax errors. There's got to be something I'm missing. It's a very simple while loop script...

#!/bin/bash
c=1
while [ $c -le 5 ]
do
    echo "Welcone $c times"
    c=$(( c++ ))
done

I should mention that I'm running bash in cygwin on windows 7.

thanks for the help

6
  • Could it be a strange Windows whitespace character? I tried running it in Linux and got the following output. Commented Jul 4, 2015 at 3:18
  • 2
    Did you use a windows editor (like Notepad) to create this file? If so, you've probably got end-of-line problems. Try using dos2unix to convert it (and read the man page, it overwrites its input). Commented Jul 4, 2015 at 3:24
  • probably is. I guess I just wont think about it too much and I'll try running it in linux when I can. (I can't install dos2unix in windows with sudo apt-get apparently... if you have a workaround I'd appreciate it ^ ^) Commented Jul 4, 2015 at 3:29
  • Use Notepad++ on windows. It has a setting for line endings (and syntax highlighting, etc). Commented Jul 4, 2015 at 3:31
  • tr -d '\r' < your-script.sh > temp.sh; mv temp.sh your-script.sh Commented Jul 4, 2015 at 3:52

1 Answer 1

2

Change:

c=$(( c++ ))

to

(( c=c+1 ))

When Bash sees: (( var)) it will try and 'do some math' on contents... In this case 'c++' == empty string == '0'; c will always be equal to '1' due to 1st assignment...

From the Bash man page on my Linux system (you may need to review this for Cygwin - could be different...):

((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".

Also:

  id++ id--  
          variable post-increment and post-decrement  
  ++id --id  
          variable pre-increment and pre-decrement  

After a little testing, the 'pre-increment' seems to do what you are after here - note that you may need to declare 'c' as an integer:

typeset -i c=1
while [ $c -le 5 ]
do
    echo "Welcone $c times"
    c=++c
#    (( c=c+1 ))
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.