0

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

And the results show:

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

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.

OK.. Doing some research: https://stackoverflow.com/a/5928254/175063

${foo/ /.}

Updating for what we may want:

${i/\ /}

The code now becomes:

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

Ref. 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
marcell
  • 1,498
  • 1
  • 10
  • 22
Leptonator
  • 3,379
  • 2
  • 38
  • 51
  • 1
    Look here: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice – iamauser Feb 01 '18 at 17:29
  • 1
    try `while read line ; do ... ; done <(find $PWD -type f -name "wp-config.php")` and replace `$i` with `$line` in the rest of your code. (not a completel solution, but should get you thinking in the right direction). Good luck. – shellter Feb 01 '18 at 17:30
  • If you expect `"'$i'"` to be `eval`-safe quoting (and are using it in a way that relies on that assumption elsewhere), you'll be deeply unhappy when you run across a file created with `d=$'$(rm -rf ~)\'$(rm -rf ~)\''; mkdir -p "$d" && touch "$d/wp-config.php"`. – Charles Duffy Feb 01 '18 at 18:51
  • BTW, more efficient to `grep DB_NAME "$i"` and not use `cat`. And see [Don't Read Lines With `for`](http://mywiki.wooledge.org/DontReadLinesWithFor) and/or [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29). – Charles Duffy Feb 01 '18 at 18:53
  • ... or even `awk -F\\ '/DB_CONFIG/{print $4}' "${i}"` – hek2mgl Feb 01 '18 at 18:54
  • 1
    I think you should start here: https://www.shellcheck.net/, fix your script and then come back here. – hek2mgl Feb 01 '18 at 18:56

1 Answers1

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

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 1
    `-d $'\x00'` is a funny way of writing `-d ''`. (And I'd argue a *misleading* one, since it implies to the reader the untrue proposition that bash strings are able to contain literal NULs; you'll note that `[[ '' = $'\x00' ]]` is true, and that both these strings have the same length as measured with `s=$'\x00'; echo "${#s}"`). – Charles Duffy Feb 01 '18 at 18:48
  • 1
    ...and should definitely make it `"$f"` on the `printf`, ofc; otherwise you're throwing out any benefits you might have gained by using NUL delimiters previously. – Charles Duffy Feb 01 '18 at 18:49
  • I prefer the explicit NUL (\x00) rather then the empty string, even if the meaning in bash is the same, to match find. – Diego Torres Milano Feb 01 '18 at 19:41
  • @CharlesDuffy f quoted. Thanks – Diego Torres Milano Feb 01 '18 at 19:42
  • I understand the added clarity about *what you're doing*, but the concern re: communicating false propositions to readers (particularly when using that code in an educational context) stands, insofar as the obvious presumption about *why* the code in question works (such being that you're specifying a length-1 string that has a NUL *as its contents*) is wrong. – Charles Duffy Feb 01 '18 at 21:21