LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2005, 04:33 PM   #1
user_linux
LQ Newbie
 
Registered: Jun 2005
Posts: 12

Rep: Reputation: 0
how to divide two string variables


I have the following unix shell script to divide two string variables

var1="50"
var2="10"
var3="$(("${var1}/${var2}"))"
echo $var3

When I run this script in Unix it runs fine. It is giving me an output of 5.

But when I try to run this same script in Linux, it gives me an error.

My question is

How do I rewrite this script so that it runs in both unix & linux?
 
Old 06-20-2005, 04:48 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
why are you inserting all these quote marks all over the place? there's simply no need to within bash.
Code:
var1=50
var2=10
var3=$((var1/var2))
echo $var3
but then the syntax is relevant to the shell, not the OS. if your unix box is on csh or ksh, then just run that shell on linux.
 
Old 06-20-2005, 04:50 PM   #3
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
Remove the quotes around ${var1} and ${var2}:
var3="$((${var1}/${var2}))"

Works for me in bash.

EDIT: acid_kewpie beat me to it...
 
Old 06-20-2005, 04:54 PM   #4
user_linux
LQ Newbie
 
Registered: Jun 2005
Posts: 12

Original Poster
Rep: Reputation: 0
thanks chris
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Divide up lines with string delimiter elmu Programming 3 10-07-2005 08:48 AM
cant divide with C skoot Programming 23 08-26-2005 06:50 PM
String variables in DDD Qt debugger impeteperry Linux - Software 0 06-12-2005 07:57 AM
Partimage & Divide by Zero KimVette Linux - Hardware 3 06-01-2005 10:18 AM
Divide a file into 2 parts bmbsa Linux - Newbie 3 08-13-2003 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 AM.

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