Hi,
With Kornshell, it is easy to embed AWK and passing variables and vaules to the AWK program. It works as follows:
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:
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:
NEWLISP_LINE='(println "${STRING}")(exit)'
newlisp -e $NEWLISP_LINE
The result is
Quote
missing parenthesis : "...(println "
Also other constructions do not work:
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
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.
btw..its newlisp -e "(env {STRING})"
That does work on the command line but not in a program, unless exporting it explicitly:
STRING="Hello1 Hello2 Hello3"
export STRING
newlisp -e "(env {STRING})"
But this works also, adding doublequotes:
STRING="Hello1 Hello2 Hello3"
NEWLISP_LINE="(silent (println {$STRING}))"
newlisp -e "$NEWLISP_LINE"
Still it's a kind of clumsy...
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}))"
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..
Quote
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?
Quote
The question is, how do I pass the variable 'STRING' to the newlisp code?
like this:
~> STRING="{Hello1 Hello2 Hello3}"
~> newlisp -e "(println $STRING)"
Hello1 Hello2 Hello3
"Hello1 Hello2 Hello3"
and this:
NEWLISP='(silent (println "hello world"))'
~> newlisp -e "$NEWLISP"
hello world
~>
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.
#!/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.
ist works if you put an extra single quotes around the defintion of STRING, if not you get 3 nil's
~> ./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"))
That works!
The result is different (AWK has double quotes, newLisp hasn't) but that's ok, of course.
Thanks again!
Peter
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})) ))"
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 ;-)