在结果中添加新换行符

时间:2018-02-01 17:15:57

标签: bash

尝试创建一个mysql备份脚本。

但是,我发现我在结果中获得了换行符:

#!/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,我们看到有问题的文件夹:

New\ website
old\ website

并将'\'视为换行符。

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

${foo/ /.}

更新我们想要的东西:

${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/

最终,我真的想要这样的事情:

!/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

1 个答案:

答案 0 :(得分:2)

You can do this

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

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