[英]Unix: “ls” command shows files with ? after the extension
I have written a shell script (test.sh) using Java. 我用Java编写了一个shell脚本(test.sh)。 This shell script actually does a copy job from one file to the other.
此shell脚本实际上从一个文件到另一个文件执行复制作业。 After the execution of the shell script I have opened the directory from Console and typed ls.
执行shell脚本后,我从Console打开了目录并键入了ls。 It shows the output file with ?
它显示输出文件? after the extension.
延期后。
example : foo.csv? 例如: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");
My guess: 我猜:
Your Java program is using \\r\\n
, carriage return + line feed, to terminate each line. 您的Java程序正在使用
\\r\\n
,回车符+换行符来终止每一行。 However, Unix systems use only the line feed \\n
as a new-line indicator. 但是,Unix系统仅使用换行符
\\n
作为换行符。 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. 虽然终端可以理解回车,但它对其他程序毫无意义 - 就
/bin/sh
解释器而言,它只是一个与其他程序一样的字符。 This means that the \\r
becomes part of the last parameter of each command that your script runs: 这意味着
\\r
成为脚本运行的每个命令的最后一个参数的一部分:
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. 因此,最后一个命令始终创建在其名称末尾具有回车符的文件,并且
ls
显示为与所有其他“不可打印”字符一样的问号。 You can see this using ls -b
or ls -Q
. 你可以使用
ls -b
或ls -Q
看到这个。
To fix this, use only \\n
when generating Unix shell scripts. 要解决此问题,请在生成Unix shell脚本时仅使用
\\n
。 (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.) (或者,更好的是, 不要生成shell脚本 - 而是编写在需要的地方接受参数的脚本。你也可以在Java程序本身做很多事情。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.