| Author |
Message |
sunburnt

Joined: 08 Jun 2005 Posts: 4014 Location: Arizona, U.S.A.
|
Posted: Wed 21 Apr 2010, 05:01 Post subject:
A way to get variables back from a function... Needed this! |
|
I found a way to get variables back out of a function. ( I`m sure others have thought of it...)
Here`s the code, could solve problems with getting variables into gtkdialog from functions:
| Code: | guiPOS() {
G=($(xwininfo -name "$1" |sed '4,9!d')) # get size and pos. for GUI
((X=G[3]-G[11], Y=G[7]-G[15], W=G[17], H=G[19]))
((X<1)) && ((X=0)); ((Y<1)) && ((Y=0)) ; echo "X=$X,Y=$Y,W=$W,H=$H"
}
overPOS() {
((`guiPOS $1`)) # get main GUI Left and top
popX=$(($X+$offX)) ; popY=$(($Y+$offY)) # get X and Y position offsets
} |
As long as the function`s returned value is one long string with no spaces it`s OK...
The evaluation parentheses take "," instead of Bashes taking ";" for evaluation separators.
How to "can" this in a library? Another words, make it call just like a function does now.
A fix for Bash`s function, call it like a normal function and get back evaluated variables.
Sort of like having the ". " run in the same shell for functions, I`d call this a big fix.
|
|
Back to top
|
|
 |
ken geometrics
Joined: 23 Jan 2009 Posts: 76 Location: California
|
Posted: Fri 23 Apr 2010, 10:55 Post subject:
|
|
Also:
If you have a list of values to return but don't want to force the names they go into, you can do something like this:
:~> CC="987654 65432"
:~> read AA BB <<-XYZZY
> $CC
> XYZZY
:~> echo "$AA"
987654
:~> echo "$BB"
65432
The $CC gets expanded into the here document. This means you can feed its contents into a command as though it came from a file.
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4014 Location: Arizona, U.S.A.
|
Posted: Fri 23 Apr 2010, 11:26 Post subject:
|
|
Hi ken geometrics; Your code looks really interesting, I`ve never seen syntax like it...
But I can`t see how it works, could you edit it into a function with it`s call below?
| Code: | f() {
# Function code
}
f ARG
# Output code |
I assume it`s doing something different than like a function returning a comma delimited string.
echo "$X,$Y,$W,$H"
|
|
Back to top
|
|
 |
potong
Joined: 06 Mar 2009 Posts: 88
|
Posted: Sat 24 Apr 2010, 07:05 Post subject:
|
|
Try these: | Code: |
# unset a b c
# a="b=bbb c=ccc"
# echo $a
b=bbb c=ccc
# eval $a
# echo $a $b $c
b=bbb c=ccc bbb ccc
# unset a b c
# echo $a $b $c
# a="b=bbb c=ccc"
# echo $a >/tmp/a
# . /tmp/a
# echo $a $b $c
b=bbb c=ccc bbb ccc
# unset a b c
# a="b=bbb c=ccc"
# . <<<$a
bash: .: filename argument required
.: usage: . filename [arguments]
|
N.B. the here-string doesn't work
Potong
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4014 Location: Arizona, U.S.A.
|
Posted: Sat 24 Apr 2010, 14:35 Post subject:
|
|
Hi potong; Variables within variables, yes... Really nifty is commands within variables!
The gtkinfo code I posted returns an info list of variables of all gtkdialogs within a variable.
|
|
Back to top
|
|
 |
|