Bash and C++, friends?

M

Magnus Jonneryd

Hi, I'm planning on writing a program that interactively is fed input via a
shell (bash). I also want to be able to write a shell script that executes
various commands related to my program. In short i want to provide input to
a program using some (or all) of the functionality found in bash.

It's mainly the format of the file I'm having trouble with. I wanted to be
able to write something like this:
#!/bin/bash

for x in xs
do
myprog.method(x)
done

How do i call myprog from a bash script (myprog is executing and reacting to
the input)?

Hopefully I've been able to convey my message.

Thanks in advance!
 
V

Victor Bazarov

Magnus said:
Hi, I'm planning on writing a program that interactively is fed input
via a shell (bash). I also want to be able to write a shell script
that executes various commands related to my program. In short i want
to provide input to a program using some (or all) of the
functionality found in bash.

It's mainly the format of the file I'm having trouble with. I wanted
to be able to write something like this:
#!/bin/bash

for x in xs
do
myprog.method(x)
done

How do i call myprog from a bash script (myprog is executing and
reacting to the input)?

I think you need to look at (a) how arguments are passed to your
programs and for that see the arguments to 'main' function and (b)
look at some primitive expression parsing.

I speculate that it should be possible to write a script to call
your program similarly to

do
myprog "method" $x
done

in which "method" will indicate which method to call and $x will
expand into something that can be internally converted into values
to pass to that method (function).

V
 
M

Magnus Jonneryd

Victor said:
I think you need to look at
(a) how arguments are passed to your programs and for that see the
arguments to 'main' function

Sure, but it's still a matter of how. How do I, in the bash script, specify
that it is myprog that should receive the input? To simply provide input at
the start of execution via the commandline isn't enough. I need to be able
to provide arguments during the programs entire execution and I want to use
some bashscripts since I don't want to invent or rather implement a new
script language.

Grateful, for input.
 
T

Tom Felker

Hi, I'm planning on writing a program that interactively is fed input via a
shell (bash). I also want to be able to write a shell script that executes
various commands related to my program. In short i want to provide input to
a program using some (or all) of the functionality found in bash.

It's mainly the format of the file I'm having trouble with. I wanted to be
able to write something like this:
#!/bin/bash

for x in xs
do
myprog.method(x)
done

How do i call myprog from a bash script (myprog is executing and reacting to
the input)?

Hopefully I've been able to convey my message.

Thanks in advance!

Well, there's no way to directly call a method from a shell script. How
you go about doing this indirectly depends on what you want. As someone
else mentioned, one way is just command line arguments. If you need the
functions to be called successively during one run of myprog, probably the
best way is to make myprog be kind of a shell itself.

The way you do this is, in the main() of myprog, have a loop that reads a
line from standard input (gets(), cin.getline()) and call the associated
function with the given arguments. Then, in a shell script, you can do
something like:

(
echo init_method
for x in xs; do
echo method $x;
done
echo other_methods
) | myprog

You could debug this by taking out the "| myprog" part, in which case the
script will print to stdout what it would have given to your program.
You can also test your program by just running it on its own and typing in
your functions manually.
 
M

Magnus Jonneryd

Tom said:
Well, there's no way to directly call a method from a shell script. How
you go about doing this indirectly depends on what you want. As someone
else mentioned, one way is just command line arguments. If you need the
functions to be called successively during one run of myprog, probably the
best way is to make myprog be kind of a shell itself.

The way you do this is, in the main() of myprog, have a loop that reads a
line from standard input (gets(), cin.getline()) and call the associated
function with the given arguments. Then, in a shell script, you can do
something like:

(
echo init_method
for x in xs; do
echo method $x;
done
echo other_methods
) | myprog

You could debug this by taking out the "| myprog" part, in which case the
script will print to stdout what it would have given to your program.
You can also test your program by just running it on its own and typing in
your functions manually.
Ok, I thought that might be the case. Was hoping to avoid parsing but it
seems unavoidable. Well, well, thanks anyway.

I realize this might be the wrong forum, but I was thinking that it might be
possible to use Python instead of shell-scripts, any thoughts?
 
R

Richard Herring

Magnus Jonneryd said:
Ok, I thought that might be the case. Was hoping to avoid parsing but it
seems unavoidable. Well, well, thanks anyway.

I realize this might be the wrong forum, but I was thinking that it might be
possible to use Python instead of shell-scripts, any thoughts?

It sounds as though you're looking for a scripting language with a C++
(or C?) API so that you can embed a script interpreter within your
application and have it execute callbacks into your code when it finds
appropriate commands in the script. I don't know enough details to make
recommendations, but Python, Perl, Rexx, Ruby and similar script
interpreters might be possible candidates.
 
M

Magnus Jonneryd

Richard said:
It sounds as though you're looking for a scripting language with a C++
(or C?) API so that you can embed a script interpreter within your
application and have it execute callbacks into your code when it finds
appropriate commands in the script. I don't know enough details to make
recommendations, but Python, Perl, Rexx, Ruby and similar script
interpreters might be possible candidates.

I was thinking along those lines and I've discovered the module/library
BOOST. Can someone that had some experience using it give me a quick review
i.e do you recommend it or not?
 
H

hajime

Hi,

Not BOOST thing. But, have you checked CINT for your scripting ?
CINT is a C/C++ interpreter. You can write your script by C/C++ that
links to your C/C++ program. Please refer
http://root.cern.ch/root/Cint.html for more details. Its not 100%
complete C/C++ interpreter though.............

Hajime
 
M

Magnus Jonneryd

hajime said:
Hi,

Not BOOST thing. But, have you checked CINT for your scripting ?
CINT is a C/C++ interpreter. You can write your script by C/C++ that
links to your C/C++ program. Please refer
http://root.cern.ch/root/Cint.html for more details. Its not 100%
complete C/C++ interpreter though.............

Hajime

Sounds really interesting, thanks.
 
R

Richard Herring

Magnus Jonneryd said:
I was thinking along those lines and I've discovered the module/library
BOOST. Can someone that had some experience using it give me a quick review
i.e do you recommend it or not?
Boost is highly recommended - for what it does, but it's not what you're
looking for here. It's not a module but a collection of relatively
low-level algorithms and utilities. There are parts which would
certainly help if you wanted to write your own parser, but that would be
a *lot* of work compared with slotting in a ready-made interpreter.
 
M

Magnus Jonneryd

Richard said:
Boost is highly recommended - for what it does, but it's not what you're
looking for here. It's not a module but a collection of relatively
low-level algorithms and utilities. There are parts which would
certainly help if you wanted to write your own parser, but that would be
a *lot* of work compared with slotting in a ready-made interpreter.

Must have misinterpreted the info, sorry. Do you have an opinion about cint
or some other interpreter (preferably for C/C++ or Python)?
 
R

Richard Herring

Magnus Jonneryd said:
Must have misinterpreted the info, sorry. Do you have an opinion about cint
or some other interpreter (preferably for C/C++ or Python)?
Sorry, I've no experience of any of them. Anyone else?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top