LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-20-2008, 12:29 AM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
bash and double precision


i have a windows powershell script i need to adapt
to run on a linux system. in it there are a number
of double precision variables. is that something
that can be done within a bash script or would i
need to move to somethig like c/c++?

if i can use double precision variables in bash,
how do i declare the variable? haven't seen that.

thanks,
BabaG
 
Old 04-20-2008, 02:17 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I don't know that bash has a notion of double precision variables,
but you can assign arbitrary values to variables, and use 'bc' to
manipulate those.


Cheers,
Tink
 
Old 04-20-2008, 04:48 AM   #3
marquardl
Member
 
Registered: Apr 2008
Posts: 100

Rep: Reputation: 15
Perl or PHP

If the script is not too complex you could translate it into a PHP or Perl script where it will be much easier to operate on double precision values. Both will work from the commandline (PHP needs to be cli-enabled).

KDE Programs Naming Convention

Last edited by marquardl; 05-01-2008 at 04:02 AM.
 
Old 04-20-2008, 02:40 PM   #4
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks for the replies. bc sounds like the way to
go for my purposes. a question, though.

i have a text file holding a lot of values. it has
been read into an array. some of the elements are
strings and some are numerical. of the numerical
ones, some are integer, some fractional. i'm having
trouble finding how i'd declare a fractional variable
from this array for piping to bc.

$GamInc=.5

if i say:

echo "$GamInc*5" | bc

i get an error, rather than the 2.5 i'd expect.
how do i get around this?

thanks again,
BabaG
 
Old 04-20-2008, 02:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
When setting variables, don't use a "$":
Code:
~$ GamInc=.5
~$ echo "$GamInc*5" | bc
2.5
The ~$ is my prompt.
 
Old 04-20-2008, 03:30 PM   #6
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
actually, to give more detail, i have my script read the
text file into the array. each line of text becomes an
element of the array. i then am assigning individual
variable names to each element so i can follow them more
clearly.

in the first gathering from the elements from the text
file, the array is called ScriptVariable[n].

i then perform a series of re-assignments, such as:

GamInc="{$ScriptVariable[75]}"

if i then say:

echo "$GamInc*5" | bc

it produces the following error:

(standard_in) 1: illegal character: ^M

something seems to be happening when i do the re-assigning
of names. if i simply echo the variable to the screen
(echo $GamInc), it looks ok. if i try to perform a
mathematical operation, it errors.

thanks,
BabaG

Last edited by babag; 04-20-2008 at 03:32 PM.
 
Old 04-20-2008, 03:38 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by babag View Post
actually, to give more detail, i have my script read the
text file into the array. each line of text becomes an
element of the array. i then am assigning individual
variable names to each element so i can follow them more
clearly.

in the first gathering from the elements from the text
file, the array is called ScriptVariable[n].

i then perform a series of re-assignments, such as:

GamInc="{$ScriptVariable[75]}"

if i then say:

echo "$GamInc*5" | bc

it produces the following error:

(standard_in) 1: illegal character: ^M

something seems to be happening when i do the re-assigning
of names. if i simply echo the variable to the screen
(echo $GamInc), it looks ok. if i try to perform a
mathematical operation, it errors.

thanks,
BabaG
You wrote your script in windows, didn't you :}


Try
Code:
fromdos < oldscript > newscript
and then work with newscript instead ...

Cheers,
Tink
 
Old 04-20-2008, 03:52 PM   #8
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks tinkster. could you elaborate on why i'd use the fromdos
command? never heard of it till now.

as i said at the start, this started as a powershell script.
i'm assuming there will be a lot of changes to convert to a
bash script. yes, the powershell version was written in windows.
i'm not directly copying it to bash, however. i have a number of
practice bash scripts for various things like reading text file
input to an array. i've copied parts of that to make this
re-written bash version. i've also combined that initial reading
of the text file with the renaming section from the powershell
script by pasting and changing some characters from the
powershell version to conform to bash syntax.

so, while parts of this did, in fact, start in windows, the
actual script being discussed at present is something of a
hybrid, mostly originating in bash. and, as i say, the echoing
to the screen works fine, it's just the piping to bc that's
erroring.

would fromdos still be appropriate in an instance like i've
described here?

thanks,
BabaG
 
Old 04-20-2008, 03:58 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by babag View Post
thanks tinkster. could you elaborate on why i'd use the fromdos
command? never heard of it till now.
fromdos/todos (or dos2unix/unix2dos in some distros)
are quite simple tools that replace the line-ends for
use in the other OS. Unix uses a plain LF, DOS (Windows)
uses a CR/LF ... the CR is what you see as ^M in your
scripts warning message. If they're not installed you
could use a simple sed-script to do that translation...

Quote:
Originally Posted by babag View Post
would fromdos still be appropriate in an instance like i've
described here?

thanks,
BabaG
I'd think so, since you're getting the error pertaining
to DOS line-ends.


Cheers,
Tink
 
Old 04-20-2008, 04:03 PM   #10
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks. looking for those now. first try produced
'command not found' for both fromdos and dos2unix.
will look for an rpm. (mandriva 2007)

thanks again,
BabaG
 
Old 04-20-2008, 04:19 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by babag View Post
thanks. looking for those now. first try produced
'command not found' for both fromdos and dos2unix.
will look for an rpm. (mandriva 2007)

thanks again,
BabaG
As I said ... sed can do that, too.

Code:
sed 's/\x0D//' oldfile > newfile
Probably quicker than a download... ;}



Cheers,
Tink
 
  


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
bash double varables whited Programming 2 10-16-2007 10:42 AM
Bash: how do I pass a double quoted string to a command? Nylex Programming 8 04-18-2007 09:36 AM
Bash - double quotes don't protect Exclamation marks geoff_f Programming 15 04-15-2007 09:10 PM
bash and double slashes MD3 Linux - Software 0 02-18-2005 03:13 PM
Bash double spaces in filenames ilikejam Programming 2 02-01-2005 10:34 AM

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

All times are GMT -5. The time now is 01:37 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