Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > arithmatic in shell script

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 2 Weeks Ago   #1 (permalink)
Just Joined!
 
Join Date: Oct 2009
Posts: 8
arithmatic in shell script

I am writing a script that outputs the fibanocci sequence and gives as many terms as specified by the argument.

#!/bin/sh
clear
i=0
a=0
b=0
echo "this is i $1"
while [ `expr $i` -lt `expr $1` ]
do
c=`expr $a+$b`
echo " $c"
a=$b
b=$c
i=`expr $i+1`
done

The error I get is this is i 6
0+0
./Fibanocci.sh: line 7: [: 0+1: integer expression expected

I'm guessing the problem is that arguments are automatically a string type so that's why I have the `expr`s inside the while loop.
blackbox111 is offline  


Reply With Quote
Old 2 Weeks Ago   #2 (permalink)
Linux User
 
dxqcanada's Avatar
 
Join Date: Sep 2006
Location: Canada
Posts: 258
Try this syntax:
Code:
#!/bin/sh
clear
i=0
a=0
b=0
echo "this is i $1"
while [ $i -lt $1 ]
do
c=$(($a+$b))
echo " $c"
a=$b
b=$c
i=$(($i+1))
done
__________________



Men occasionally stumble over the truth,
but most of them pick themselves up
and hurry off as if nothing had happened.

Winston Churchill


... then the Unix-Gods created "man" ...
dxqcanada is offline   Reply With Quote
Old 2 Weeks Ago   #3 (permalink)
Linux Newbie
 
Join Date: May 2008
Location: NYC, moved from KS & MO
Posts: 247
You can't start a fibanocci with two 0's.

Code:
#!/bin/bash
[ $# -lt 1 ] && echo Usage: $0 count && exit 0
a=0
b=1
echo -n $a $b
for i in `seq 1 $1`; do
    c=$((a+b))
    echo -n " "$c
    a=$b
    b=$c
done
echo
secondmouse is offline   Reply With Quote
Old 2 Weeks Ago   #4 (permalink)
Just Joined!
 
Join Date: Oct 2009
Posts: 8
thanks that did it.
why was using expr wrong and what happens when you put the expression in $(...) ?
blackbox111 is offline   Reply With Quote
Old 2 Weeks Ago   #5 (permalink)
Linux Newbie
 
Join Date: May 2008
Location: NYC, moved from KS & MO
Posts: 247
For now you don't have to worry about type conversion in bash, types are automatically decided unless you specify (declare) otherwise. For example:

Code:
$ a=3
$ b="4"
$ [ $a -lt $b ] && echo yes
yes
Regarding $((..)), please read Arithmetic Expansion
secondmouse is offline   Reply With Quote
Old 2 Weeks Ago   #6 (permalink)
Linux Newbie
 
Join Date: Mar 2009
Posts: 136
Quote:
Originally Posted by blackbox111 View Post
thanks that did it.
why was using expr wrong and what happens when you put the expression in $(...) ?

Your expr's weren't working because you didn't have spaces around the '+' sign.

Code:
c=`expr $a+$b`
should be:
c=`expr $a + $b`

i=`expr $i+1`
should be:
i=`expr $i + 1`
lomcevak is offline   Reply With Quote
Old 2 Weeks Ago   #7 (permalink)
Just Joined!
 
clickalot's Avatar
 
Join Date: Nov 2009
Location: Erlangen, Germany
Posts: 31
Lightbulb bc - An arbitrary precision calculator language

you can also try to have a look at the bc "command"

an example usage from a script:
Code:
percent=`echo "100 * $counter / $total_lines" | bc -l` # calculate percent
clickalot is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 05:31 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2