Menu

#32 Directly get the first line of a shell command output into a variable

Unstable (example)
open
Patches (4)
3
2019-02-07
2019-02-07
astx
No

Attached a POC for a new command: "setcmd variablename = shellcommand shellargs"
Directly get the first line of a shell commands output. Only a POC, untested, unchecked if secure, unchecked for memory leaks ... I'm not a c programmer ...
It would be nice if multiline results are supported too as array ...

An example console output:

ngspice 8427 -> shell cat t.pl
#!/usr/bin/perl
printf("%f\n", 5.38135e+006/1000000);

ngspice 8428 -> setcmd myvar=perl ./t.pl
setcmd: 1 <myvar=perl ./t.pl>
setcmd: 2 <myvar> <perl ./t.pl>
setcmd: 3 var=<myvar> result=<5.381350>

ngspice 8429 -> set
...
myvar 5.381350
...

BR, Toni

1 Attachments

Related

Patches: #32

Discussion

  • marcel hendrix

    marcel hendrix - 2019-02-07

    On 2019-02-07 17:08, astx wrote:


    [patches:#32] Directly get the first line of a shell command output
    into a variable

    Status: open
    Group: Unstable (example)
    Labels: Patches
    Created: Thu Feb 07, 2019 04:08 PM UTC by astx
    Last Updated: Thu Feb 07, 2019 04:08 PM UTC
    Owner: Holger Vogt
    Attachments:

    -
    ngspice_poc_add_setcmd_command_to_directly_send_shell_output_to_variable.diff
    (4.7 kB; text/x-patch)

    Attached a POC for a new command: "setcmd variablename = shellcommand
    shellargs"
    Directly get the first line of a shell commands output. Only a POC,
    untested, unchecked if secure, unchecked for memory leaks ... I'm not
    a c programmer ...
    It would be nice if multiline results are supported too as array ...

    An example console output:
    ~~~
    ngspice 8427 -> shell cat t.pl

    !/usr/bin/perl

    printf("%f\n", 5.38135e+006/1000000);

    ngspice 8428 -> setcmd myvar=perl ./t.pl
    setcmd: 1 <myvar=perl t.pl="" .="">
    setcmd: 2 <myvar> <perl t.pl="" .="">
    setcmd: 3 var=<myvar> result=<5.381350></myvar></perl></myvar></myvar=perl>

    ngspice 8429 -> set
    ...
    myvar 5.381350
    ...

    ~~~

    Did you know this one:

    ngspice 1 -> set a=`dc -e '3 4 * p'`
    ngspice 2 -> echo $a
    12
    ngspice 3 ->
    

    -marcel

     

    Related

    Patches: #32

  • astx

    astx - 2019-02-07

    No - didn't know, that backticks are working partially. Couldn't find any hint in the manual. Sorry I haven't read it completely line by line - only searched for "backtick".
    Fine that it's supported! For a single value it is usable, but - compared are the results here on string operation:

    ngspice 8464 -> set myvar1=`/bin/bash -c "echo $curplottitle"`
    ngspice 8465 -> setcmd myvar2=/bin/bash -c "echo $curplottitle"
    setcmd: 1 <myvar2=/bin/bash -c "echo Constant values">
    setcmd: 2 <myvar2> </bin/bash -c "echo Constant values">
    setcmd: 3 var=<myvar2> result=<Constant values>
    ngspice 8466 -> set
    ...
    myvar1        Constant
    myvar2        Constant values
    ...
    

    BR, Toni

     
  • astx

    astx - 2019-02-07

    Aaahhh: in the manual at 17.10 there is a hint for "backquote" - I searched for "backtick"
    ... the corresponding file is "src/frontend/parser/backq.c"

    ngspice 8512 -> set myvar1=`/bin/bash -c "echo '( 1 2 3 4 5 )'"`
    ngspice 8513 -> setcmd myvar2=/bin/bash -c "echo '( 1 2 3 4 5 )'"
    setcmd: 1 <myvar2=/bin/bash -c "echo '( 1 2 3 4 5 )'">
    setcmd: 2 <myvar2> </bin/bash -c "echo '( 1 2 3 4 5 )'">
    setcmd: 3 var=<myvar2> result=<( 1 2 3 4 5 )>
    ngspice 8514 -> set
    ...
    myvar1        ( 1 2 3 4 5 )
    myvar2        ( 1 2 3 4 5 )
    ...
    

    Look same, but isn't (CP_LIST versus CP_STRING)
    With backquotes the myvar1 is converted to an array, with poc "setcmd" the string stays untouched.

    ngspice 8529 -> echo $myvar1[2]
    2
    ngspice 8530 -> echo $myvar2[2]
    
    ngspice 8531 ->
    

    String/array handling is very confusing ...
    IMHO: if a program returns a string it should stay a string. To convert a string to an array a function should be used. E.g.: perl uses the "split" command to convert a string to an array.

    Will take a closer look to the backquote/backtick feature.

    BR, Toni

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.