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

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?

Related

How to execute gvim command using c++

I need to write a code to jump to a particular given line number in gVim. But I need to do this using a C++. Is there any way to do it?
There are ways to open the gvim by executing the Linux commands using C++ code. But is there a way to execute the gVim command using the same C++ code?
Do you mean the function system that runs linux commands? You can use it for any command and command parameters. Bellow is the example, modify it according to your requirements.
int line_no = 576;
std::string file_name;
std::stringstream command;
command << "gvim +" << line_no << " " << filename;
system(command.str().c_str());
It will execute gvim +576 file_name, that opens a file at a particular line in gvim.
You can ask vim to run commands from the command line. See the list of vim command line options.
To position cursor on the line num run vim +num filename, for example vim +578 filename jumps to line 578 in the named file. vim + filename jumps to the last line.
Search: vim +/pattern filename will position cursor on the first line containing "pattern".
Other commands: vim -c command filename executes the ex (:) command. You can run up to 10 commands (up to 10 -c options).

How to use putty to run a .sas program via a .bat file

I'm using two files:
1- .bat file which contains in one line:
"C:\Path_to\putty.exe" -ssh servername -l myuserid -pw my password -m "C:\Path_to_Commands_File\Commands.txt" -t
2- Commands.txt which contains either one of these commands:
Command#1:
/Path_to_sasprogram/sasprogram.sas
OR
Commands#2 in the following order:
cd /Path_to_sasprogram/
sas sasprogram.sas
However, the sasprogram.sas does not get executed. So, I used putty to manually execute the commands above. With the commands#2, I get this error: "sas: command not found".
Any suggestions/help would be greatly appreciated! BTW, I tried some solutions already posted, but they didn't work for me. Thanks
It sounds like sas is not on the path of your session.
try
/path_to_sas_executable/sas sasprogram.sas

Want to run a cpp executable in a new terminal and then send a file into the input stream

I would like to run a c++ executable in a new linux terminal, which I am doing using:
xterm -e executable options &disown
and this works. However, I also need to parse a text file through the command line. Normally, the file would be parsed by:
./executable options < inputFile.txt
and then the file is handled by the c++ code using this function:
void parse_lines(istream &in){
verify_version_number(in);
read_variables(in);
...
}
However the following line does not work:
xterm -e executable options < inputFile.txt &disown
How can I run the executable in a new terminal and then send the contents of inputFile.txt into the istream?
Thanks!
If you put quotes around the command, it will be sent to the shell as a single command, and the special characters < and & will be interpreted in the shell running within xterm rather than in the shell where you start xterm:
xterm -e "executable options < inputFile.txt &disown"
Try x-terminal-emulator instead of xterm. Perhaps it works.

C++ program cannot find latex command with system() function call

I am writing a C++ program which has to automatically generate some data to be used by students in an integrated exercise. I have already exported this data to .tex files and would also like the C++ program to be able to compile these tex files automatically.
Usually I would compile tex files from the command line by doing the following:
$ latex file.tex
$ latex file.tex
$ dvipdf file.dvi
So I tried doing the following in my C++ code (directory and filename are both strings):
//Move to the location where I created the files
string mycommand = "cd ";
mycommand += directory;
system(mycommand.c_str());
//Compile latex
mycommand = "latex " + filename + "_main.tex";
system(mycommand.c_str());
system(mycommand.c_str());
//Create .pdf
mycommand = "dvipdf " + filename + "_main.dvi";
system(mycommand.c_str());
Which then produces the following error message on the terminal output:
sh: latex: command not found
sh: latex: command not found
sh: dvipdf: command not found
I have searched this online but I have failed to find a solution for this problem, though I believe it is likely to be something very simple.
I am working on OSX and have the following version of latex installed:
pdfTeX 3.1415926-2.4-1.40.13 (TeX Live 2012)
kpathsea version 6.1.0
All help is greatly appreciated!
First, the path to the programs latex and dvipdf needs to be in your PATH environment variable.
Second, the calls of the shell via system are totally independent (in fact a new instance of the shell is started each time). So if you switch the directory in one, this does not affect the others. Switch instead the current directory of your program via:
chdir(directory.c_str())
You need for this
#include <cunistd>
using namespace std;
at the beginning of the file.
Please note that system calls with command lines depending on input parameters can be easily exploited to run arbitrary commands if the parameters (in your case the filename) are not carefully checked. As you do not have quotes, the program fails if there are e.g. whitespaces in the filename.

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

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