[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Share variables between Shell and Octave
From: |
Judd Storrs |
Subject: |
Re: Share variables between Shell and Octave |
Date: |
Mon, 22 Nov 2010 12:53:30 -0500 |
On Mon, Nov 22, 2010 at 7:03 AM, 张彤 <address@hidden> wrote:
> I have a question that is it possible to share variable between Shell
> and Octave, and if so, HOW?
It depends on what you mean by share and what type of values you want
to communicate? Here are some examples in bash:
1) You can use environment variables:
export a=4
octave -q --eval 'disp(sin(str2num(getenv("a"))))'
or equivalently
a=4 octave -q --eval 'disp(sin(str2num(getenv("a"))));'
2) Octave's script mode (#!/usr/bin/octave) can use the argv()
function to get command line parameters:
myscript.m:
#!/usr/bin/octave -q
args=argv() ;
a=str2num(args{1}) ;
b=str2num(args{2}) ;
disp(exp(-i*a))
disp(sin(b))
bash$ chmod +x ./myscript.m
bash$ ./myscript.m 2 3
3) --eval works well to do things in a more inline fashion in bash
without creating various script files just to compute simple things:
a=4
b=$(octave -q --eval "disp(sin($a))")
echo $b
4) For slightly longer inline scripts you can use a here file, which
also lets you incorporate command substitution:
a=5
b=7
octave -qf <<EOF
disp(exp(-i*$a))
disp(sin($b))
disp("$(pwd -P)")
EOF
--judd