Page 1 of 1
Howto embed newlisp in (shell) programs?
Posted: Sat Apr 05, 2008 6:07 am
by pjot
Hi,
With Kornshell, it is easy to embed AWK and passing variables and vaules to the AWK program. It works as follows:
Code: Select all
STRING="Hello1 Hello2 Hello3"
awk -v VAR="$STRING" 'BEGIN {print VAR}'
This is a stupid example of course, but the idea is that you are able to pass values from a main program to a piece of AWK code.
Now, I want to do the same thing with newLisp. I have a main program in Kornshell and I want to pass some strings to a piece of newLisp code.
This line gives the same result as the AWK example just mentioned:
Code: Select all
newlisp -e "(silent (println \"Hello1 Hello2 Hello3\"))"
The question is, how do I pass the variable 'STRING' to the newlisp code? I thought of constructing the newLisp code as a string in advance, but it does not work:
Code: Select all
NEWLISP_LINE='(println \"${STRING}\")(exit)'
newlisp -e $NEWLISP_LINE
The result is
missing parenthesis : "...(println "
Also other constructions do not work:
Code: Select all
NEWLISP_LINE="(println $STRING)(exit)"
NEWLISP_LINE='(println $STRING)(exit)'
They all deliver the same error.
The workaround is to rewrite the main Kornshell program completely to newLisp, but then I have to convert over a 2000 lines of code. :-(
So, how can I embed newLisp code within a larger (shell) program, and pass values and variables to these codesnippets from the main program?
Thanks
Peter
Posted: Sat Apr 05, 2008 6:22 am
by newdep
Good point as I tried that also several times but its not very charming..
I you indeed would like to embed newLisp as it where Perl or Awk is
not very handy.. A thing i did once was making the code base64, but
that not readble ;-)
Would be very nice to have a way to simply embed newlisp in shell scripting
indeed... some extension to [text][/text].
like this should not complain about brackes etc...
$newlisp -e [text](sys-info)[/text]
-bash: syntax error near unexpected token `('
or
$newlisp -e {(sys-info)}
Should simply evaluate.. So actualy newlisp need a better parser on the command line ;-)
best would be directly ->
$newlisp -e (begin (sys-info))
Norman.
Posted: Sat Apr 05, 2008 6:35 am
by newdep
btw..its newlisp -e "(env {STRING})"
Posted: Sat Apr 05, 2008 6:49 am
by pjot
That does work on the command line but not in a program, unless exporting it explicitly:
Code: Select all
STRING="Hello1 Hello2 Hello3"
export STRING
newlisp -e "(env {STRING})"
But this works also, adding doublequotes:
Code: Select all
STRING="Hello1 Hello2 Hello3"
NEWLISP_LINE="(silent (println {$STRING}))"
newlisp -e "$NEWLISP_LINE"
Still it's a kind of clumsy...
Posted: Sat Apr 05, 2008 7:03 am
by newdep
I think i dont get it..this works inside bash..
#!/usr/bin/bash
STRING="Hello1 Hello2 Hello3"
awk -v VAR="$STRING" 'BEGIN {print VAR}'
newlisp -e "(0 6(env {STRING}))"
Posted: Sat Apr 05, 2008 7:10 am
by newdep
but this does not bring me what i want.. ;-)
#!/usr/bin/bash
STRING="Hello1 Hello2 Hello3"
awk -v VAR="$STRING" 'BEGIN {print VAR}'
newlisp -e "(env {STRING2} (string (0 6 (env {STRING}))))"
awk -v VAR="$STRING2" 'BEGIN {print VAR}'
and thats because we are dealing with a subshell issue..
Posted: Sat Apr 05, 2008 7:37 am
by pjot
I think i dont get it..this works inside bash..
#!/usr/bin/bash
STRING="Hello1 Hello2 Hello3"
awk -v VAR="$STRING" 'BEGIN {print VAR}'
newlisp -e "(0 6(env {STRING}))"
For me it doesn't... probably you still are in the same shell since the beginning, where you set the variable STRING on the prompt...?
If you start a new shell, does this piece of code still work?
Posted: Sat Apr 05, 2008 7:58 am
by Lutz
The question is, how do I pass the variable 'STRING' to the newlisp code?
like this:
Code: Select all
~> STRING="{Hello1 Hello2 Hello3}"
~> newlisp -e "(println $STRING)"
Hello1 Hello2 Hello3
"Hello1 Hello2 Hello3"
and this:
Code: Select all
NEWLISP='(silent (println "hello world"))'
~> newlisp -e "$NEWLISP"
hello world
~>
Posted: Sat Apr 05, 2008 8:01 am
by pjot
That is not embedding newLisp in a shell program, that is starting newLisp from the command prompt.
So how do I pass a variable WITHIN a shell program?
You guys are setting the variable on the shell prompt, thereby automatically exporting it.
So, please start a new shell and copy and paste this into a new program.
Code: Select all
#!/bin/ksh
#
# Testing embedded newLisp
#
STRING="Hello1 Hello2 Hello3"
awk -v VAR="$STRING" 'BEGIN {print VAR}'
newlisp -e "(println $STRING)"
Then exit your editor and run it with ksh, because it is a Kornshell program. (Other shells have the same problem, try with bash for example.) You'll see that AWK works, but that newLisp returns nil.
So, how do I pass the variable 'STRING' to the newlisp code within a larger shell program?
I can export it explicitly and use (env), or construct the newLisp code as a string in advance, but this is all kind of clumsy. But there seems to be no other way.
Posted: Sat Apr 05, 2008 9:34 am
by Lutz
ist works if you put an extra single quotes around the defintion of STRING, if not you get 3 nil's
Code: Select all
~> ./test
"Hello1 Hello2 Hello3"
Hello1 Hello2 Hello3
~> cat test
#!/bin/ksh
#
# Testing embedded newLisp
#
STRING='"Hello1 Hello2 Hello3"'
awk -v VAR="$STRING" 'BEGIN {print VAR}'
newlisp -e "(silent (println $STRING ))"
~>
I also tried with bin/bash, which also works. Remember that the shell expands literally into: (silent (println Hello1 Hello2 Hello3)) This is why we need the extra singgle quotes they make everhthinh inside the expansion: (silent (println "Hello1 Hello2 Hello3"))
Posted: Sat Apr 05, 2008 9:44 am
by pjot
That works!
The result is different (AWK has double quotes, newLisp hasn't) but that's ok, of course.
Thanks again!
Peter
Posted: Tue Apr 08, 2008 9:36 am
by newdep
Extra quotes?
You can use {} in newlisp... (advantage: the environment variable doesnt
have to be manipulated with extra quotes...)
#!/bin/bash
#
# Testing embedded newLisp
#
STRING="This can be anywhere"
awk -v VAR="$STRING" 'BEGIN {print "awk =>" VAR}'
newlisp -e "(silent (println {newlisp =>} {$STRING} ))"
STRING=ready?
newlisp -e "(silent (println {newlisp =>} {$STRING} ))"
STRING=10
newlisp -e "(silent (println (* 10 (int {$STRING})) ))"
Posted: Tue Apr 08, 2008 10:07 am
by newdep
even better.. but risky if you dont know the TYPE of the content of
the variable.. (Bash/ksh convert to string and Integer)
STRING=10
newlisp -e "(silent (println {newlisp =>} (* 10 $STRING) ))"
instead of
newlisp -e "(silent (println (* 10 (int {$STRING})) ))"
STRING=255
echo "bash =>" $(( 2 * STRING ))
echo "bash & newlisp =>" $(( STRING * `newlisp -e "$((STRING * STRING))"`))
echo "or like this =>" $(( STRING * `newlisp -e "(* 20 20)"` ))
echo "or =>" $(( 1024 * `newlisp -e $STRING`))
STRING=`newlisp -e "(div 22 7)"`
echo "newlisp again => " `newlisp -e "(silent (println (* $STRING 5)))"`
Get it ;-)