简体   繁体   English

如何在所有目录中的特定文件之间循环shell脚本?

[英]How to loop a shell script across a specific file in all directories?

Shell Scripting sed Errors: Shell脚本sed错误:

Cannot view /home/xx/htdocs/*/modules/forms/int.php 无法查看/home/xx/htdocs/*/modules/forms/int.php

/bin/rm: cannot remove `/home/xx/htdocs/tmp.26758': No such file or directory / bin / rm:无法删除`/home/xx/htdocs/tmp.26758':无此类文件或目录

I am getting an error in my shell script. 我的shell脚本出现错误。 I am not sure if this for loop will work, it is intended to climb a large directory tree of PHP files and prepend a functions in every int.php file with a little validation. 我不确定此for循环是否能正常工作,它打算爬上PHP文件的大型目录树,并在每个int.php文件中添加一个函数,并进行一些验证。 Don't ask me why this wasn't centralized/OO but it wasn't. 不要问我为什么这不是集中式的/ OO,但不是。 I copied the script as best I could from here: http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/ 我尽可能从此处复制脚本: http : //www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

#!/bin/bash
OLD="public function displayFunction(\$int)\n{"
NEW="public function displayFunction(\$int)\n{if(empty(\$int) || !is_numeric(\$int)){return '<p>Invalid ID.</p>';}"
DPATH="/home/xx/htdocs/*/modules/forms/int.php"
BPATH="/home/xx/htdocs/BAK/"
TFILE="/home/xx/htdocs/tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do 
 if [ -f $f -a -r $f ]; then
   /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
 else
  echo "Error: Cannot view  ${f}"
 fi
done
/bin/rm $TFILE

Do wildcards like this even work? 这样的通配符还能用吗? Can I check in every subdirectory across a tree like this? 我可以像这样在树上签入每个子目录吗? Do I need to precode an array and loop over that? 我是否需要对数组进行预编码并对其进行循环? How would I go about doing this? 我将如何去做呢?

Also is, the $ in the PHP code breaking the script at all? 同样,PHP代码中的$根本破坏了脚本吗?

I am terribly confused. 我非常困惑。

Problems in your code 您的代码中的问题

  • You cannot use sed to replace multiple lines this way. 您不能使用sed这样替换多行。
  • you are using / in OLD which is used in a s/// sed command. 您在s/// sed命令中使用的OLD中使用/ This won't work 这行不通
  • [ ! -d $BPATH ] && mkdir -p $BPATH || : [ ! -d $BPATH ] && mkdir -p $BPATH || : is horrible. [ ! -d $BPATH ] && mkdir -p $BPATH || :太可怕了。 use mkdir -p "$bpath" 2>/dev/null 使用mkdir -p "$bpath" 2>/dev/null
  • Yes, wildcards like this will work but only because your string has no spaces 是的,这样的通配符将起作用,但是仅因为您的字符串没有空格
  • Doube-quote your variables, or your code will be very dangerous 用双引号引用您的变量,否则您的代码将非常危险
  • Single quote your strings or you won't understand what you are escaping 用单引号括住您的字符串,否则您将不明白自己在逃脱什么
  • Do not use capital variable names, you could accidentally replace a bash inner variable 不要使用大写变量名称,您可能会意外替换bash内部变量
  • do not rm a file that does not exist 不要管理不存在的文件
  • Your backups will be overwritten as all files are named int.php 您的备份将被覆盖,因为所有文件都命名为int.php

Assuming you are using GNU sed, I'm not used to other sed flavors. 假设您使用的是GNU sed,我不习惯其他sed风格。 If you are not using GNU sed, replacing the \\n with a newline (inside the string) should work. 如果您不使用GNU sed,则应该用换行符(在字符串内)替换\\n

Fixed Code 固定码

#!/usr/bin/env bash
old='public function displayFunction(\$int)\n{'
old=${old//,/\\,} # escaping eventual commas
# the \$ is for escaping the sed-special meaning of $ in the search field
new='public function displayFunction($int)\n{if(empty($int) || !is_numeric($int)){return "<p>Invalid ID.</p>";}\n'
new=${new//,/\\,} # escaping eventual commas
dpath='/home/xx/htdocs/*/modules/forms/int.php'
for f in $dpath; do 
    [ -r "$f" ]; then
        sed -i.bak ':a;N;$!ba;'"s,$old,$new,g" "$f"
    else
       echo "Error: Cannot view  $f" >&2
    fi
done

Links 链接

暂无
暂无

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

相关问题 如何使用根目录中存在的所有目录下特定文件夹中的Shell脚本列出所有文件? - How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory? shell脚本循环通过通配符匹配的目录 - shell script to loop through wildcard matched directories 更新所有目录中的Shell脚本更改 - Update a shell script change in all directories Bash:如何在所有子目录中的所有名为output.log的文件中搜索特定短语 - Bash: How to search for a specific phrase in all files named output.log across all sub directories 如何使用shell脚本创建多个目录 - How to create multiple directories using shell script 如何使用 linux 中的 bash shell 脚本递归地重命名文件中的所有目录、文件和文本 - How to rename all directories , files and text in the files recursively using bash shell script in linux 如果特定用户无法写入目录,则 Linux shell 脚本会检查目录 - Linux shell script check in an directory if directories are not writeable by specific user 使用shell脚本除去最后5个目录,然后查找命令 - remove all but last 5 directories with shell script and find command 如何使用Linux Shell脚本读取文本文件列并将文件复制到子目录中的另一个路径 - How to read the column of text file using Linux shell script and copy the files to another path with in sub directories 列出所有用户目录并查找特定文件 - List all user directories and look for specific file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM