bash 如何使用 shell 脚本自动化 dos2unix?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11292010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:39:36  来源:igfitidea点击:

How to automate dos2unix using shell script?

bashshelldos2unix

提问by Alan Smith

I have a bunch of xml files in a directory that need to have the dos2unix command performed on them and new files will be added every so often. I Instead of manually performing dos2unix command on each files everytime I would like to automate it all with a script. I have never even looked at a shell script in my life but so far I have this from what I have read on a few tutorials:

我在一个目录中有一堆 xml 文件,需要对它们执行 dos2unix 命令,并且每隔一段时间就会添加新文件。每次我想用脚本自动执行所有文件时,我都不想在每个文件上手动执行 dos2unix 命令。在我的生活中,我什至从未看过 shell 脚本,但到目前为止,我从一些教程中读到了这个:

FILES=/tmp/testFiles/*
for f in $FILES
do
  fname=`basename $f`
  dos2unix *.xml $f $fname
done

However I keep getting the 'usage' output showing up. I think the problem is that I am not assigning the name of the new file correctly (fname). Can anyone help.

但是,我一直在显示“使用情况”输出。我认为问题在于我没有正确分配新文件的名称(fname)。任何人都可以帮忙。

Thanks, Alan

谢谢,艾伦

采纳答案by Paused until further notice.

The reason you're getting a usage message is that dos2unixdoesn't take the extra arguments you're supplying. It will, however, accept multiple filenames (also via globs). You don't need a loop unless you're processing more files than can be accepted on the command line.

您收到一条使用消息的原因是dos2unix它没有采用您提供的额外参数。但是,它将接受多个文件名(也通过 globs)。除非您处理的文件多于命令行可接受的数量,否则您不需要循环。

dos2unix /tmp/testFiles/*.xml

Should be all you need, unless you need recursion:

应该是你所需要的,除非你需要递归:

find /tmp/testFiles -name '*.xml' -exec dos2unix {} +

(for GNU find)

(用于 GNU 查找)

回答by sorpigal

If all files are in one directory (no recursion needed) then you're almost there.

如果所有文件都在一个目录中(不需要递归),那么您就快到了。

for file in /tmp/testFiles/*.xml ; do
    dos2unix "$file"
done

By default dos2unixshould convert in place and overwrite the original.

默认情况下dos2unix应该就地转换并覆盖原始文件。

If recursion is needed you'll have to use findas well:

如果需要递归,您还必须使用find

find /tmp/testFiles -name '*.xml' -print0 | while IFS= read -d '' file ; do
    dos2unix "$file"
done

Which will work on all files ending with .xmlin /tmp/testFiles/and all of its sub-directories.

这将适用于所有以.xmlin/tmp/testFiles/及其所有子目录结尾的文件。

If no other step are required you can skip the shell loop entirely:

如果不需要其他步骤,您可以完全跳过 shell 循环:

Non-recursive:

非递归:

find /tmp/testFiles -maxdepth 1 -name '*.xml' -exec dos2unix {} +

And for recursive:

对于递归:

find /tmp/testFiles -name '*.xml' -exec dos2unix {} +

In your original command I see you finding the base name of each file name and trying to pass that to dos2unix, but your intent is not clear. Later, in a comment, you say you just want to overwrite the files. My solution performs the conversion, creates no backups and overwrites the original with the converted version. I hope this was your intent.

在您的原始命令中,我看到您找到了每个文件名的基本名称并尝试将其传递给dos2unix,但您的意图尚不清楚。后来,在评论中,您说您只想覆盖文件。我的解决方案执行转换,不创建备份并用转换后的版本覆盖原始版本。我希望这是你的意图。

回答by Igor Chubin

mkdir /tmp/testFiles/converted/
for f in /tmp/testFiles/*.xml
do
  fname=`basename $f`
  dos2unix $f ${f/testFiles\//testFiles\/converted\/}
  # or for pure sh:
  # dos2unix $f $(echo $f | sed s@testFiles/@testFiles/converted/@)
done

The result will be saved in the converted/subdirectory.

结果将保存在converted/子目录中。

The construction ${f/testFiles\//testFiles\/converted\/}(thanks to Rush) or sedis used here to add converted/before the name of the file:

构造${f/testFiles\//testFiles\/converted\/}(感谢Rush)或sed在此处用于在converted/文件名之前添加:

$ echo /tmp/testFiles/1.xml | sed s@testFiles/@testFiles/converted/@
/tmp/testFiles/converted/1.xml

回答by Erwin Waterlander

It is not clear which implementation of dos2unix you are using. Different implementations require different arguments. There are many different implementations around.

不清楚您正在使用哪种 dos2unix 实现。不同的实现需要不同的参数。周围有许多不同的实现。

On RedHat/Fedora/Suse Linux you could just type

在 RedHat/Fedora/Suse Linux 上,您只需键入

    dos2unix /tmp/testFiles/*.xml

On SunOS you are required to give an input and output file name, and the above command would destroy several of your files.

在 SunOS 上,您需要提供输入和输出文件名,上述命令会破坏您的多个文件。

Which version are you using?

您使用的是哪个版本?

regards, Erwin

问候,欧文