ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 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.
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).
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?
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.
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 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 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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.