Linux的shell脚本 - 找到所有文件,并在其中的每一个运行命令 [英] Linux shell script - find all files and run a command on each one of them

查看:137
本文介绍了Linux的shell脚本 - 找到所有文件,并在其中的每一个运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图端口的Windows批处理文件到Linux shell脚本(bash)的。

I'm trying to port a windows batch file to a linux shell script (bash).

下面是给我一些头痛的部分:

Here is the part that is giving me some headache:

for /r %%G in (*Metadata*.xml) DO (
java -jar %SAXON%\saxon9he.jar -o:%%~nG.csv "%%G" %WORKDIR%\transformBQT.xsl)

这样做是找到包含文本的的,然后在这些文件上运行的XSLT转换所有.xml文件。这需要3个参数

What this does is find all .xml file containing the text Metadata and then running the an XSLT transformation on each of these files. This takes 3 arguments


  • -o是输出文件(这将是具有相同的名称的.xml为.csv)

  • 接下来是目标文件

  • 最后一个参数是的.xsl文件

我想用下面的:

find /results/ -type f -name "*Metadata*.xml" -exec 
java -jar $SAXON/saxon9he.jar -o:??? {} $WORKDIR/transformXMI.xsl

但是这完全不是那么回事,因为我不知道如何使输出文件具有相同的名称的.xml(带.csv扩展名)

but this doesn't quite work as I don't know how to make the output file have the same name as the .xml (with .csv extension)

任何提示?

推荐答案

您可以处理来自一行找到线,改造&LT的结果;文件>的.xml <文件>的.csv

You could process the results from find line by line and transform <file>.xml into <file>.csv:

find /results/ -type f -name "*Metadata*.xml" | while read file; do java -jar $SAXON/saxon9h3.jar -o:${file%.xml}.csv $file $WORKDIR/transform.XMI.xsl; done

这个简单的方法的情况下,文件名有自己的路径/名称空间将失败。

This simple approach fails in case the file names have spaces in their paths/names.

这篇关于Linux的shell脚本 - 找到所有文件,并在其中的每一个运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆