Dos create a batch file and run with multiple C++ programs - c++

Respected sirs,
My name is #nimit. I want to create a batch file and run it in a DOS prompt. The batch file will execute a C++ program I've written. The output should be stored in a single text-file. How can I do this?
The C++ program output should be stored in a particular text file.
Thanks in advance,
#nimit

You can do this:
programname > outputgoeshere.txt
To collect outputs:
programname1 >> outputgoeshere.txt
programname2 >> outputgoeshere.txt
programname3 >> outputgoeshere.txt

Shell scripting (Batch files are a form of that) is something that every programmer should know how to do. I found a really great book on it a few years ago: Unix Shell Programming by Stephen Kochan and Patrick Wood. Granted, it's Unix -- and bash is far more powerful than DOS, but the principles are the same. Windows is picking up a lot of the tools that bash offers with powershell.
For a great website that lists out all of the CMD programs, visit http:// ss64.com/nt/ . That site also lists comparable bash and powershell commands. I also like how he shows you how to implement pseudo-functions, command line parameters, and all manner of cool things in batch files: http://ss64.com/nt/syntax.html
Good luck!

The following will redirect program output (stdout) to a file (overwrite file or create it if it does not exist)
$ command-name > output.log
The following will redirect program output (stdout) to a file (append file or create it if it does not exist)
$ command-name >> output.log
$ command-name >> output.log
The following will redirect program error message to a file called error.log:
$ command-name 2> error.log
Redirecting the standard error (stderr) and stdout to file, Use the following syntax:
$ command-name &> output_error.log

Related

Ubuntu shell script: Using >> the output txt file is not opening

I using the following python script to create a shell script:
with open("decode_JOURNAL2017.sh", "a") as myfile:
levels_no = 16
#myfile.write("#!/bin/bash\n")
for x in range(0, levels_no):
myfile.write("/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx "+str(x)+" -o"+str(x)+" /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt\n")
Then I transfer the following created script to an ubuntu server:
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 0 -o0 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_0_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_0_dec.txt
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 1 -o1 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_1_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_1_dec.txt
/home/zinonas/SHM-12.3/bin/TAppDecoderStatic -b /home/zinonas/str/pirkagia_10b_lowdelay_P.bin -olsidx 2 -o2 /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_2_dec.yuv >> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_2_dec.txt
When I run it, the txt files are created but when I double click to one to open it I get this message:
Can't create file 'C:\Users\admin\AppData\Local\Temp\scp43940\home\zinonas\decode\pirkagia_10b_lowdelay_P_level_0_dec.txt
'.
System Error. Code: 123.
The filename, directory name, or volume label syntax is incorrect
I can't even transfer this txt file to my windows desktop via winscp.
The yuv files are working properly!
EDIT: When I copy/paste the content of the shell script to the terminal, the txt files are working properly.
EDIT 2: When I rename the file and remove txt and add it again, the file opens properly...
Do you know how to fix this?
Thank you in advance!
You probably have Windows line endings in the script file – at least that's why I get from the error message if you pasted it correctly.
Run dos2unix scriptFilename.sh or sed -i 's/\r//' scriptFilename.sh to remove the Windows line endings from the script.
For Python scripting refer to this Question&Answer: How to write Unix end of line characters in Windows using Python
I found the solution.
While creating the shell script, next to the name of the txt file I had:
...
>> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt\n")
so the new line character \n want next to txt. That was the problem. I added a new space between them and the problem solved!
My new code now reads:
...
>> /home/zinonas/decode/pirkagia_10b_lowdelay_P_level_"+str(x)+"_dec.txt \n")
Is there a "rogue" CR at the end of the file name?

system("history") not working

I've run into a snag, I'm trying to implement a linux shell program of sorts with C++ and many of my commands seem to work, however, when I try to get the history(list all recently executed commands) I get an error of "sh: 1: history: not found" the below line is all that runs in the area, what is the issue?
system("history"); //produces the error above ^
If I do
$ history
from the command line it's fine...why is it not fine in C++?
system executes a program using /bin/sh, but history is a bash builtin.
You might look at the contents of ~/.bash_history instead. (Note (by leemes) .bash_history is only updated after closing a previous bash session, as well as it is not updated by executing a command with system.)
Because it's a bash shell builtin not necessarily accessible through /bin/sh -c` (which may be the bourne shell).

How do I open a terminal window with C++ in Ubuntu?

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.

How to work in batch mode

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

Redirecting C++ generated cerr and cout to the same file in Ubuntu

My program generates output both directed to std::cout and std::cerr. When I run a test file normally as in
./a.out <source> &> <target>
target captures both types of output in the order produced by ./a.out.
When trying to automate testing of output for a simple unit testing framework, I implemented the above in a bash script:
`rm $OUT_NAME`
`./a.out $NEW_UNIT &> $OUT_NAME`
(with the obvious variable names). The output sent to cout is printed fine; but the one sent to cerr is printed incorrectly (some is printed; then printing stops with no error). In case you wonder, I added the 'rm' first just to be perfectly sure it's no issue with over-writing/appending to an old version.
Any ideas?
My system:
Ubuntu 12.04.
In bash, you should do the following:
./a.out source > target 2>&1
to merge stderr into stdout. The command you gave is meant for csh.
And if you want to merge stdout into stderr, you will do
./a.out source 2> target 1>&2
./a.out source >> target 2>&1 would solve your problem
2>&1 means that combine(or called redirect) standard error (stderr) with standard out (stdout)
>> means to attach the output stream of your program to then end of "target". And if you use a single > instead of >>, it means to replace the output stream of your program with the "target".
P.S.
Suppose your code have two different output
std::cout<<"results"<<endl
and some std::cerr<<"debug information"<<endl
In bash, you can do ./a.out source 1>>Result.txt 2>> Debug.txt
And your "results" would be store in Result.txt and your "debug information" would be store in Debug.txt.
1>> means standard output redirect to the back of somewhere, you can also use 1> which means standard out redirect to somewhere
2>> means standard error redirect to the back of somewhere, you can also use 2> which means standard error redirect to somewhere
That would be very useful if you want to use script automatically run your program.
In bash, this should work:
./a.out source >target 2>&1