Treating variables with space in it as a command.

We know that var=rgb2hsv and putting in $var would convert images to HSV color space. What about var="colormap 32"?

That’s a good question.
Something as:

foo :
  sp colorful
  command="colormap 32"
  $command

won’t work because $command is parsed as a single item before being substituted, and it cannot magically becomes two items.

One simple solution is:

foo :
  sp colorful
  command="colormap 32"
  run $command
3 Likes

Neat.

Also, I learned that you can do this:

foo:
str="m=${2-3} status $m"
run $str
echo ${}

That’s a long waited answer to one of my question.

Aw, this won’t work:

foo:
a=2
b=3
str=m=${$a-$b}" status $m"
run $str
echo ${}

@David_Tschumperle For some unknown reason you tend to answer some of my questions before I even ask. And it’s not the first time…

1 Like

Because :

  • ${$a-$b} will be substituted by ${2-3}, itself (recursively) substituted by trying to get status from command 2-3 which does not exist obviously (syntax recognized as ${"command"}).

Solution:

  • Actually, I don’t know what you want to do here. For instance, if you want to access the value of argument $-1 (last argument), then you’re in a case where you want to access the value of an argument that is unknown during the command instanciation, and it’s not possible to deal with that except by using $=arg which is the expression that set variables from arguments:
foo : 
  $=arg
  a=2
  b=3
  ind:=($a-$b)%($#+1)
  str="m="${arg$ind}" status $m"
  run $str
  echo ${}

then:

$ gmic foo 1,2,3
[gmic]./ Start G'MIC interpreter (v.3.3.1).
3
[gmic]./ End G'MIC interpreter.

But I doubt this is something you really want to do in practice !

The reason I want this is because I wouldn’t have to resort to using a combination of $=arg and repeat n, and the use of .= operator and comma to combine arguments when I want variable selection of user-input argument.

So, is the example you provided limited to just 1 argument?

In my example yes, but if you need more complex selections, there is another solution : define your own (temporary) command that returns whatever you want, then call it with the original parameters.
Like in this example:

foo :
  a=2
  b=5
  m "temp : u ${"$a-$b"}" # Return a subset of arguments
  temp $"*"
  echo ${}
  um temp

then:

$ gmic foo 1,2,3,4,5,6,7,8
[gmic]./ Start G'MIC interpreter (v.3.3.1).
2,3,4,5
[gmic]./ End G'MIC interpreter.

The key idea here is that you can define your own command where the command code depends itself of some variables you have computed before.

2 Likes