简体   繁体   English

在bash文件中运行多个查找和sed

[英]Running multiple find and sed in a bash file

I have the code below 我有下面的代码

find . -type f -exec sed -i  's#<![endif]>##g' {} +
find . -type f -exec sed -i  's#<script src="/js/vendor/modernizr-2.6.2.min.js?v=201425100529"></script>##g' {} +   
find . -type f -exec sed -i  's# <!--[if lt IE 9]>##g' {} +       

in a bash file. 在bash文件中。

If I run the lines directly in terminal it works, but If I run them together in a sh file I have an error: 如果我直接在终端中运行这些行,则可以,但是如果我在一个sh文件中一起运行它们,则会出现错误:

find: missing argument to '-exec' 查找:缺少'-exec'的参数

The reason the command execution succeeds but the script failed is, command执行成功但script失败的原因是:

when the script gets executed the find command searches for all files and directories in the current execution path ( as . is used in find ). 当脚本被执行的find为在当前执行路径中的所有文件和目录的命令搜索(如.在使用find )。 Again this also includes the script itself. 同样,这还包括script本身。 This creates the script to be overwritten/modified by the sed . 这将创建要被sed覆盖/修改的脚本。

And so instead of keeping the script in the same directory when the file edits needs to be done, the script can be kept in some other directory and an absolute path can be give to the find command. 因此,当需要进行文件编辑时,不必将脚本保存在同一目录中,而是可以将脚本保存在其他目录中,并可以将absolute path提供给find命令。

And it is also recommended to terminate commands with \\; 并且还建议使用\\;终止命令\\; to indicate the end of arguments. 表示参数的结尾。

Always use bash to execute scripts instead of sh which means bourne shell . 始终使用bash而不是sh来执行脚本,这意味着bourne shell Generally bash will be a symlink for sh but it will run in a compatibility mode which causes bash to loose modern functions. 通常bashsh的符号链接,但它将以兼容模式运行,这会导致bash失去现代功能。

#!/bin/bash

find /Absolute/path -type f -exec sed -i  's#<!\[endif\]>##g' '{}' \;
find /Absolute/path -type f -exec sed -i  's#<script src="/js/vendor/modernizr-2.6.2.min.js?v=201425100529"></script>##g' '{}' \;
find /Absolute/path -type f -exec sed -i  's# <!--\[if lt IE 9\]>##g' '{}' \; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM