Recent

Author Topic: Runcommand - how to pass quotes  (Read 4149 times)

DavidPowell

  • Newbie
  • Posts: 3
Runcommand - how to pass quotes
« on: June 07, 2017, 01:52:10 am »
Hi All,

I am very new to lazarus, but used to delphi. I have ported across a lazarus program to a mac, and all seems fairly well, except that I need to get some info on gateway IPs and MAc addresses.

This command from a terminal window works fine:

route -n get default | grep "gateway"

But when I do a runcommand to get the output into a variable tempstr with

runcommand('route -n get default | grep "gateway"',tempstr)

it compiles fine, but comes back with an empty string.

runcommand('route -n get default', tempstr)

works fine, but isnt quite what I want.

I have tried experimenting with single quotes, and square brackets but have not made it work yet. I suspect it may require an eval command, but am unsure how to use it.

Can anyone guide me?

Many thanks for your patience!

DAvid

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Runcommand - how to pass quotes
« Reply #1 on: June 07, 2017, 02:12:38 am »
Can anyone guide me?

RunCommand runs a single command. You want to run two commands and pipe the output of the first to the second.

Try this:

RunCommand('bash -c ''route -n get default | grep "gateway"''', TempStr);

And note that RunCommand is deprecated.

http://wiki.freepascal.org/Executing_External_Programs#.28Process..29RunCommand


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Runcommand - how to pass quotes
« Reply #2 on: June 07, 2017, 02:58:29 am »
And note that RunCommand is deprecated.
With the addition that only that version of runcommand is deprecated, see also documentation.
Quote
The version using CmdLine attempts to split the command line in a binary and separate command-line arguments. This version of the function is deprecated.

In addition to Phil's suggestion, you could also attempt to run the route command directly and filter the output (outputstr) from the command manually.

DavidPowell

  • Newbie
  • Posts: 3
Re: Runcommand - how to pass quotes
« Reply #3 on: June 08, 2017, 12:48:01 am »
Thanks for the comments, that is great, now working fine.  (even though I dont fully understand how your routing command works, it works!).

David.

 

TinyPortal © 2005-2018