Bash and C++, friends?

Discussion in 'C++' started by Magnus Jonneryd, Aug 13, 2005.

  1. 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)
     
    Magnus Jonneryd, Aug 13, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 13, 2005
    #2
    1. Advertising

  3. 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)
     
    Magnus Jonneryd, Aug 13, 2005
    #3
  4. Magnus Jonneryd

    Tom Felker Guest

    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, <>
    <http://vlevel.sourceforge.net> - Stop fiddling with the volume knob.

    Code is speech!
     
    Tom Felker, Aug 15, 2005
    #4
  5. 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)
     
    Magnus Jonneryd, Aug 15, 2005
    #5
  6. In message <>, Magnus Jonneryd
    <> 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
     
    Richard Herring, Aug 16, 2005
    #6
  7. Richard Herring wrote:

    > In message <>, Magnus Jonneryd
    > <> 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)
     
    Magnus Jonneryd, Aug 16, 2005
    #7
  8. Magnus Jonneryd

    hajime Guest

    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
     
    hajime, Aug 16, 2005
    #8
  9. 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)
     
    Magnus Jonneryd, Aug 16, 2005
    #9
  10. In message <>, Magnus Jonneryd
    <> writes
    >Richard Herring wrote:
    >
    >> In message <>, Magnus Jonneryd
    >> <> 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
     
    Richard Herring, Aug 17, 2005
    #10
  11. Richard Herring wrote:

    > In message <>, Magnus Jonneryd
    > <> writes
    >>Richard Herring wrote:
    >>
    >>> In message <>, Magnus Jonneryd
    >>> <> 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)
     
    Magnus Jonneryd, Aug 18, 2005
    #11
  12. In message <>, Magnus Jonneryd
    <> writes
    >Richard Herring wrote:
    >
    >> In message <>, Magnus Jonneryd
    >> <> writes
    >>>Richard Herring wrote:
    >>>
    >>>> In message <>, Magnus Jonneryd
    >>>> <> 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
     
    Richard Herring, Aug 18, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,301
    William Park
    Jul 14, 2005
  2. Buster Copley
    Replies:
    5
    Views:
    561
    Gianni Mariani
    Jul 7, 2003
  3. Replies:
    2
    Views:
    600
  4. Replies:
    0
    Views:
    658
  5. Spiros Bousbouras

    Reading from files and range of char and friends

    Spiros Bousbouras, Mar 10, 2011, in forum: C Programming
    Replies:
    123
    Views:
    1,985
    James Kuyper
    Mar 31, 2011
Loading...

Share This Page