重击换行结果 [英] Bash new line feed in results

查看:60
本文介绍了重击换行结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建mysql备份脚本.

Trying to create a mysql backup script.

但是,我发现结果中出现换行符:

However, I am finding that I am getting line feeds in the results:

#!/bin/bash
cd /home
for i in $(find $PWD -type f -name "wp-config.php" );
do echo "'$i'";
done

结果显示:

'/home/site1/public_html/folders/wp-config.php'
\'/home/site2/public_html/New'
'Website/wp-config.php'
'/home/site3/public_html/wp-config.php'
'/home/site4/public_html/old'
'website/wp-config.php'
'/home/site5/public_html/wp-config.php'

在命令行中执行ls,我们看到有问题的文件夹:

Do a ls from the command-line, we see for the folders in question:

New\ website
old\ website

,并将'\'视为换行符.

and is treating the '\' as newline character.

好..做一些研究: https://stackoverflow.com/a/5928254/175063

${foo/ /.}

更新我们可能想要的东西:

Updating for what we may want:

${i/\ /}

代码现在变为:

#!/bin/bash
cd /home
for i in $(find $PWD -type f -name "wp-config.php" |${i/\ /});
do echo "'$i'";
done

参考 https://tomjn.com/2014/03/01/wordpress-bash-magic/

最终,我真的想要这样的东西:

Ultimately, I really want something like this:

!/bin/bash
# delete files older than 7 days
## find /home/dummmyacount/backups/ -type f -name '*.7z' -mtime +7 -exec rm {} \;
# set a date variable
DT=$(date +"%m-%d-%Y")
cd /home
for i in $(find $PWD -type f -name "wp-config.php" );
WPDBNAME=`cat $i | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat $i | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat $i | grep DB_PASSWORD | cut -d \' -f 4`
do echo "$i";
#do echo $File;
#mysqldump...
done

推荐答案

您可以这样做

find . -type f -name "wp-config.php" -print0 | while read -rd $'\x00' f
do
    printf '[%s]\n' "$f"
done

使用NUL字符作为分隔符以避免特殊字符

which uses the NUL character as the delimiter to avoid special chars

这篇关于重击换行结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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