LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-28-2015, 07:28 PM   #1
dinoprogrammer
LQ Newbie
 
Registered: Aug 2015
Posts: 2

Rep: Reputation: Disabled
Probably giving lots of folks a good laugh


I'm an older programmer who is trying to figure out shell scripts on Linux. I'm trying to do something very simple but having a very difficult time figure out what I'm doing wrong. I've looked for answers here ane elsewhere but nothing has really shown me the folly of my ways.

I'm running Fedora 17 (I know I should upgrade) on a Compaq laptop and have created the following shell script in vi:

#!/bin/bash

a=0
while [ $a -lt 3 ]
do
echo "$a"
a=a+1
done


When I run it I get the following error:

./whileexample: line 4: [: a+1: integer expression expected

I've tried it with $a+1 and both change the error message to 0+1 from a+1

If someone could please explain what I'm doing wrong and why it's wrong I'd really appreciate it.

Thank you,

dinoprogrammer
 
Old 08-28-2015, 07:35 PM   #2
ardvark71
LQ Veteran
 
Registered: Feb 2015
Location: USA
Distribution: Lubuntu 14.04, 22.04, Windows 8.1 and 10
Posts: 6,282
Blog Entries: 4

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
Probably giving lots of folks a good laugh
Hi...

Welcome to the forum

There's nothing funny about finding the difficulties of others amusing. Please don't think that I or others on this forum feel that way.

Regards...
 
Old 08-28-2015, 08:14 PM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
This has some examples of how to increment in bash. You need to use arithmetic expansion.
http://askubuntu.com/questions/38552...riable-in-bash
 
Old 08-28-2015, 08:26 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
No one is laughing, we all know the feeling!

You can't do simple arithmetic in a shell script as you might expect.

For details see the bash man page, man bash, and find the syntax and usage for ((expression)) and "let" and the section on ARITHMETIC EVALUATION...

Quote:
((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".
What this means for your example is try this instead...

Code:
#!/bin/bash
And welcome to LQ!

a=0
while [ $a -lt 3 ]
do
echo "$a"
#a=a+1
((a=a+1))
done
...where I have commented out the original line.

In
 
Old 08-29-2015, 03:43 AM   #5
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,487

Rep: Reputation: Disabled
Welcome aboard.
 
Old 08-29-2015, 04:28 AM   #6
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Nobody's laughing, but we may just chuckle... when we think of the fact that we've done it too.

Last edited by goumba; 08-29-2015 at 04:48 AM. Reason: "we done it too" lol
 
Old 08-29-2015, 04:43 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by astrogeek View Post
No one is laughing, we all know the feeling!
Just wanted to second this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Folks Library 0.9.4 Squashes Lots of Bugs LXer Syndicated Linux News 0 08-17-2013 04:51 AM
About.com: What is an EFI file? -- Good for a laugh. tux9656 General 4 09-30-2012 01:52 PM
I got a good laugh out of this Jmcatch742 General 19 07-25-2005 02:08 AM
a good link, if you like a laugh bigjohn General 3 09-19-2003 02:43 AM
good for a laugh... titanium_geek General 4 06-07-2003 12:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration