0

I want to send the C/C++ variable into my shell script, but tried many ways on the Internet, seems not working, could you help me with my code? C++ code:

void test::addroute(){
string test1=ip_address;
}

my shell script:

#!bin/sh
ip rule add from $test1 lookup $eth0_table

how to send the test1 to here?

run code
  • 11
  • 2
  • 1
    It very much depends on your OS and on the particular use case (why do you need that? how many variables are passed? What kind of variables do you need?) – Yksisarvinen Dec 04 '19 at 12:45
  • You can run `ip` from your C code directly. e.g. `system("ip rule add...");` – rustyx Dec 04 '19 at 12:50

1 Answers1

0

You can print the variable content on stdout and include it with using subcall $().

So if you program will be called test. Just call it like

ip rule add from $(test) lookup ... 

If you need more complex output, make it a csv-like format and use it like this:

output="$(test)"
first="$(echo ${output}|cut -d ';' -f 1 )"
second="$(echo ${output}|cut -d ';' -f 2 )"
ip rule add from "${first}" lookup "${second}"

Don't know if changing variables directly would make a bit of chaos.

Ondřej Kolín
  • 1,348
  • 1
  • 11
  • 15