I am developing an engineering program with WxWidgets and Codeblocks on Windows that dependent some Bash scripts.
I want to run the program with a seperate Bash window not Windows Command Prompt (to print out colored outputs to Bash console).
Is there any EASY way to run commands asynchronously with WxWidgets, because the samples that came with Wx-2.8 is contains 1231 lines of code and i dont have much more time.
wxShell(_("bash --login -i")); // This locks the program until bash window closed
wxShell(_("bash -c grep bla bla")); //This works great but on CMD but
// I want to run on bash.exe like that
wxShell(_("egrep bla bla"));
You need the function wxExecute with with flag wxEXEC_ASYC. Something like:
wxExecute(_("bash --login -i"), wxEXEC_ASYNC);
should do what you need. If you want to redirect input and output then you will also need wxProcess.
Related
I'm making a basic music player in C++ on an embedded Linux platform (Intel Edison).
It works by sending commands to the shell to play the music using mpg123.
Playing the music is working fine, but now I need to add media controls. I'm using the mpg123 command to play my MP3 files and if you pass it the -C flag then it listens for commands such as p for pause, q for quit, etc
I'm using the code from this other SO answer for executing the commands How to execute a command and get output of command within C++ using POSIX?
Now the thing is, when I run the command to play the selected song from c++ (eg. mpg123 -C /media/sdcard/Music/<songname>) it hangs the c++ program until the command returns, which doesn't return until the song is done playing. I can eliminate that issue by using pthead to run the command in a parallel thread, but that still doesn't allow me to enter commands as I need to.
I thought of maybe somehow running the command in another tty, and then somehow passing the commands to mpg123 by sending them to the other tty, but I was unable to find a way to do this. (and I'm not sure it's possable)
I've been trying to do this for hours, so now I'm ready to ask here for some help :)
Do any of you have any ideas on how to run the command without freezing the thread (in a pthread or whatever), and still be able to send commands to mpg123?
It sounds like you'll want to use mpg123's FIFO mode. You set a pathname to be used, point mpg123 at it and then echo your commands into it.
This answer has a great example
FIFO_MPG='/srv/http/newsctl'
mpg123 -R --fifo "$FIFO_MPG"
echo 'load filename.mp3' >> "$FIFO_MPG"
I have a image processing project in C++ using opencv. The program runs correctly and I get the desired output. However, I have some messages that I print out using the cout command. When I run the program using the terminal (./myprogram) the messages are displayed correctly. When I double click the executable file I get only the output (in my case a new video is created) But I do get the messages. How do I get the program to automatically prompt the messages when it is not run from the terminal.
PS: I use ubuntu 14.04
Create a script like this, lets call it run.sh:
#!/bin/sh
cd work_dir
./myProgram
read -r -p "Press any key..." key
Then do:
xterm -e run.sh
and make your desktop shortcut run this command instead of the program directly.
I recently decided to start teaching myself C++ and thought a simple encryption project would be a good place to start, since it covers most of the basics (cout, cin, opening files, etc). Is there a way to have the code open a terminal window similar to the one opened when I compile and run from sublime text?
I have tried this so far, but it hasn't changed anything.
string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system(cmd.c_str());
Essentially, I would like to be able to run the program by clicking the .exe, and have the terminal where all of the input and output goes pop up.
You don't need to write any code, you just need to configure the shortcut to launch the program in a terminal. Here's a Gnome dialog that shows that option:
Problem seems to be gnome-terminal, or then just my failure to give it the right arguments. For example gnome-terminal -x sh -c 'ls -l ; exec bash' from command line in another terminal just opens an empty gnome-terminal and spits out a bunch of glib warnings to original terminal... (Note to readers: if you can give the right command that works for gnome-terminal, please let me know in comments or just edit this paragraph.)
However, using xterm works, for example xterm -e sh -c 'ls -l; exec bash', or a line for your code:
string cmd = "xterm -e sh -c 'ls -l; exec bash'";
As a side note, the command to open the default x terminal window of the DE is x-terminal-emulator, but it quite often has the practical problem of different terminals taking different arguments, so sadly you're probably better of using a specific terminal, like that xterm, and requiring that to be installed, or letting user to configure what terminal to use, with what arguments (though letting user to specify any command to be run can also be a security risk, if user is not always trusted).
Just be very careful with escaping. For example, when you test the command form command line, and then copy-paste it to C++ string literal, you need to escape every " and \ one more time for C++. If you have trouble with this, check out C++11 raw strings.
Escaping becomes extra important if you construct the command string at runtime, and especially if you accept user input and add that to the string. In that case, better search for and use some existing library like GLib, or sanitize the user input very carefully (ie. just paranoidically reject anything with chars, which may have a special meaning in shell in some context).
If you are actually asking, how can my program open a console window for itself similar to how Windows console programs behave, and redirect it's own stdin, stdout and stderr there, as if it was launched from command line, that that is not very easy from the same binary, and it is not commonly done like that in Unix.
If you want a behaviour like that, you could create a desktop shortcut, but more general way is to write a wrapper shell script, which starts your binary in a terminal. What kind of script exactly, depends on how you want it to behave exactly: what will it do with stdio, will it return or wait for program to exit, how do you want it to find the binary, how does it behave when run from command line instead of double-clicking from GUI, etc.
I have inherited an ANSI C++ program that: has no GUIs and is supposed to run in batch mode, generating lots of data (we are talking 100,000+ ASCII files). We are thinking that in long term we’ll run it under UNIX. For now, I have a MacBook Air running OS X 10.9.4 and I loaded Xcode 5.1.1. It compiles without errors or warnings.
I need to test a program as follows:
<prompt> myprogram datain dataout1 datout2
Where is the compiled program? In which directory? Can I copy my datain file in that directory?
For repeated execution under Windows (Command Prompt window) I normally would have a batch file of the type:
myprogram datain1 dataout11 datout12
myprogram datain2 dataout21 datout22
myprogram datain3 dataout31 datout32
........
myprogram datainn dataoutn1 datoutn2
Can I do something similar with OS X? Where can I find the applicable documentation?
You will want to look for your terminal emulation program. See http://en.wikipedia.org/wiki/Terminal_(OS_X) for how to use it, and it should be the bash shell which is one of the unix shells
You can also do a shell script see
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html for some bash shell scripting info
For such a simple operation, you can write a shell script that will look almost exactly the same as the batch file you use on Windows. The key difference between Windows' cmd.exe and *nix shells here is that the current directory is not part of the search path for executables (the way it is on Windows), so if you put the shell script in the same folder as the compiled executable, you will need to prefix the program name with ./ (to mean "look in the current directory"). For example:
#!/bin/sh
./myprogram datain1 dataout11 datout12
./myprogram datain2 dataout21 datout22
./myprogram datain3 dataout31 datout32
........
./myprogram datainn dataoutn1 datoutn2
If the shell script and executable are not in the same folder, you can use either an absolute path or an appropriate relative path.
Also, to run the script you will either need to make it executable:
$ chmod +x myscript.sh
$ ./myscript.sh
or invoke the shell with the script as an argument:
$ sh myscript.sh
I tried the following code, to communicate with the command line from c++ code.
#include<iostream>
#include<cv.h>
int main()
{
system("gnome-terminal");
system("cd");
}
The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory
, but it did not. am working in linux
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
If I try ls, it seems to be working fine!
My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
The system function creates a shell child process to execute the specified command.
cd is a shell command which changes the current working directory of that shell process only.
So the child's cd probably works fine, but it has no effect on your C++ program, which is a different process.
Instead, you probably want to look at the Linux system call chdir.
Thanks for your help!! This command worked perfectly fine from this link
https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
gnome-terminal -x sh -c 'command1; command2; exec bash'
and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.