Reputation:
I have written a shell script (test.sh) using Java. This shell script actually does a copy job from one file to the other. After the execution of the shell script I have opened the directory from Console and typed ls. It shows the output file with ? after the extension.
example : foo.csv?
File execFile = new File(file);
FileWriter fwFile;
try {
fwFile = new FileWriter(execFile);
execFile.setExecutable(true);
BufferedWriter bwFile = new BufferedWriter(fwFile);
bwFile.write(strOutput.substring(0, 2));
bwFile.write("\r\n");
bwFile.write("cd " + strOutput);
bwFile.write("\r\n");
bwFile.write("mkdir " + strOutput);
bwFile.write("\r\n");
bwFile.write(strUnixPath);
bwFile.write("\r\n");
bwFile.write("cd " + strWorkingPath + IPlatinumConstants.FS+"lib"+IPlatinumConstants.FS+"Unx");
bwFile.write("\r\n");
bwFile.write("echo Cut Src Start time %time%");
bwFile.write("\r\n");
bwFile.write("cp " + " \"" + strSourceFilePath + "\" \""
+ strOutput + "copy_A\"");
bwFile.write("\r\n");
Upvotes: 5
Views: 2099
Reputation: 16532
My guess:
Your Java program is using \r\n
, carriage return + line feed, to terminate each line. However, Unix systems use only the line feed \n
as a new-line indicator. While the carriage return is understood by terminals, it has no meaning at all to other programs – it's just a character like any other as far as the /bin/sh
interpreter is concerned. This means that the \r
becomes part of the last parameter of each command that your script runs:
cd myoutput\r
mkdir myoutput\r
cd something/lib/something\r
echo Cut Src Start time %time%\r
cp "sourcefile" "myoutputcopy_A"\r
So the last command is always creating files that have the carriage-return at the end of their name, and ls
shows them as a question mark like all other "unprintable" characters. You can see this using ls -b
or ls -Q
.
To fix this, use only \n
when generating Unix shell scripts. (Or, even better, don't generate shell scripts – instead, write scripts that accept parameters where needed. You could do many things in the Java program itself, as well.)
Upvotes: 13