1

I've been using a program of mine written in bash to interact with a mysql database, I switched to microsoft sql server and now I have a very strange issue. The code below worked with mysql. With microsoft sql server I can see that it successfully pulls the count. My "echo $id" shows a value of 23 like it should but the issue is bash spits out " Syntax error: Bad for loop variable". I'm confused at why it's doing that 23 is an integer value. Please Help.

 id="`tsql -S Server\\SqlServerName -U Databas_Name -P Password -o q <<EOF
 use numbers
 go
 SELECT COUNT(*) FROM lotsa_numbers
 go
 quit
 EOF`"

 echo $id
 for (( c=0; c=>$id-1; c++ ))
 do
      echo $c
 done
3
  • 1
    Out of curiosity, why are you doing the for loop this way, rather than c<=$id? Commented Jan 31, 2014 at 3:44
  • Also, to narrow this down, try putting id=$(echo $id) before the for loop (this will truncate leading and trailing whitespace, etc...) Commented Jan 31, 2014 at 3:46
  • BroSlow, thanks for your help if you post the echo $id issue as a solution I'll check box you, that did exactly what I needed and you deserve credit. also I have the -1 because I'm dealing with an index of 0. Commented Jan 31, 2014 at 3:49

1 Answer 1

1

Issue was likely leading or trailing whitespace. Number of ways to deal with this, a simple one is using bash splitting by not quoting a variable (might cause a problem in some cases, but not if we're trying to get an integer)

id=$(echo $id)
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.