用户如何在 bash 脚本中将文件转换为 Linux 格式?

Jen*_*nny 2 command-line bash scripts

我的目录中有很多*.c文件。*.h我想将所有这些文件转换为Linux兼容的格式。我尝试执行以下脚本并且它运行但没有任何转换。

我还需要检查所有内容是否都已成功转换。因此,我对其输出进行过滤并将其与目录中的原始文件进行比较。

我该如何解决它?

#!/bin/bash

function  converting_files() {

   cd "/path/to/dir" &&  find . -type f -print0 | xargs -0 | dos2unix

}

function success_of_converting_files() {

 FILES=colsim_xfig/xfig.3.2.5c/*
 #There are 250 files in the dir and not all but most of them are .c and .h
 for i in {000..249} ; do                 
   for f in "$Files" ; do 
   #I think *.txt line ending is fine as we only want to compare
     dos2unix < myfile{i}.txt | cmp -s - myfile{i}.txt
   done 
   done        
}

function main() {

   converting_files
   success_of_converting           
}
Run Code Online (Sandbox Code Playgroud)

我基本上需要将所有文件转换为LF行结尾。pS:目录中的文件总数为 249。目录中的文件数量不固定,因此,如果我可以有任意数量的参数而不是只有 249,那就更好了。

ste*_*ver 5

在命令中

cd "/path/to/dir" &&  find . -type f -print0 | xargs -0 | dos2unix
Run Code Online (Sandbox Code Playgroud)

您正在将一个以空分隔的文件名列表通过管道传输到xargs,但不提供在它们上运行的命令。在这种情况下,xargs默认对它们执行/bin/echo:换句话说,它只是在标准输出上输出以空格分隔的文件名列表,然后将其通过管道传输到dos2unix. 结果是,您只需转换文件名列表,而不是将文件转换为 Unix 格式。

大概你的意图是

cd "/path/to/dir" &&  find . -type f -print0 | xargs -0 dos2unix
Run Code Online (Sandbox Code Playgroud)

find但是,您可以使用命令-exec-execdir例如更紧凑地实现相同的效果

find "/path/to/dir/" -type f -execdir dos2unix {} +
Run Code Online (Sandbox Code Playgroud)

或(限制为.c.h文件)

find "/path/to/dir/" -type f -name '*.[ch]' -execdir dos2unix {} +
Run Code Online (Sandbox Code Playgroud)