471,075 Members | 706 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Bash and C++, friends?

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!

--
(Should insert humorous quotation here)
Aug 13 '05 #1
11 2854
Magnus Jonneryd wrote:
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
Aug 13 '05 #2
Victor Bazarov wrote:
Magnus Jonneryd wrote:
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


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.

--
(Should insert humorous quotation here)
Aug 13 '05 #3
On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:
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.

--
Tom Felker, <tf******@uiuc.edu>
<http://vlevel.sourceforge.net> - Stop fiddling with the volume knob.

Code is speech!

Aug 15 '05 #4
Tom Felker wrote:
On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:
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.

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?
--
(Should insert humorous quotation here)
Aug 15 '05 #5
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Tom Felker wrote:
On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:
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.

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.

--
Richard Herring
Aug 16 '05 #6
Richard Herring wrote:
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Tom Felker wrote:
On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:

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.

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.


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?

--
(Should insert humorous quotation here)
Aug 16 '05 #7
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

Aug 16 '05 #8
hajime wrote:
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.
--
(Should insert humorous quotation here)
Aug 16 '05 #9
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Richard Herring wrote:
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Tom Felker wrote:

On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:

> 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.

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.


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.

--
Richard Herring
Aug 17 '05 #10
Richard Herring wrote:
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Richard Herring wrote:
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Tom Felker wrote:

> On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:
>
>> 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.
>
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.


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.


Must have misinterpreted the info, sorry. Do you have an opinion about cint
or some other interpreter (preferably for C/C++ or Python)?
--
(Should insert humorous quotation here)
Aug 18 '05 #11
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Richard Herring wrote:
In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
Richard Herring wrote:

In message <43******@news.wineasy.se>, Magnus Jonneryd
<ma*************@ipbolaget.com> writes
>Tom Felker wrote:
>
>> On Sat, 13 Aug 2005 17:44:26 +0200, Magnus Jonneryd wrote:
>>
>>> 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.
>>
>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.
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.


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?

--
Richard Herring
Aug 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bernhard Kuemel | last post: by
5 posts views Thread by Phil Powell | last post: by
3 posts views Thread by John Bowling | last post: by
4 posts views Thread by Tom Purl | last post: by
16 posts views Thread by John Salerno | last post: by
6 posts views Thread by Ishpeck | last post: by
6 posts views Thread by Frantisek Malina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.