473,770 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2996
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.sourcefo rge.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3493
by: Bernhard Kuemel | last post by:
Hi! To relief the problems of accessing a unix machine from behind a restrictive firewall or from an internet cafe I started to make a PHP web interface to bash. I'd like to hear your opinions and advice about my concept, especially regarding security. There is already such a thing (http://www.rohitab.com/cgiscripts/cgitelnet.html). However, it lacks interactive input to programs. To fix this I'd use a frame
5
2997
by: Phil Powell | last post by:
I'm sorry but I can't figure out how to explain this any better than this. In PHP we have a command "require()" that obtains a file and logically places it into another file. I cannot figure out how to do this in bash script as the requirement is necessary for a migration script to obtain the code from a .cfg file and then be able for the "parent" script to run the code it "imported" from the .cfg file, much like PHP's require() or...
3
8258
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but does not print the file to the printer. It displays the following as the return from Runtime...: java.lang.UNIXProcess@1034bb5
2
2984
by: Eric Woudenberg | last post by:
I just installed a Python 2.3.4 Windows binary on a friend's WinXP machine (because the latest Cygwin-provided Python 2.3 build leaves out the winsound module for some reason). When I try and run this newly installed Python from bash it just hangs (no version message, no prompt, CPU is idle). When I run it from IDLE, or the DOS CMD prompt, it runs fine. Also, Python scripts using the new version run fine, even invoked on the bash command...
4
3275
by: Tom Purl | last post by:
I just wrote a Python script that is going to be called from bash script. If the Python script fails, I want the bash script to also stop running. Unfortunately, I can't seem to get that to work. Here's the script so far: # custom python script /usr/bin/python /home/username/Dev/Python/packZODB.py \ -s"gsgmc.sigma.zettai.net" -b 7 # next program
16
3947
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of the normal bash commands (e.g. print instead of echo). My main reason for asking is that I like using Python for everything, and if I don't need to learn the bash 'language', then I won't just yet. Thanks.
6
3946
by: Ishpeck | last post by:
I'm using Python to automate testing software for my company. I wanted the computers in my testing lab to automatically fetch the latest version of the python scripts from a CVS repository and then ask a local database server which of the scripts to run. I built the following: #!/bin/bash # Batcher will run the specified scripts.
4
3580
by: melmack3 | last post by:
Hello My PHP script executes many bash/cmd commands. Functions like "exec()" or "system()" cause that new bash/cmd session is started, the command is executed and the session is closed. Unfortunately it is very slow process so I would like to increase performance and open one bash/cmd session on the begin of my script and execute the commands such as in normal system opened bash/cmd window and close it
6
1870
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions I need to write a program that creates real application/FTP accounts
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.