[SOLVED] Probably giving lots of folks a good laugh
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.