2

First, I'm sorry for my inglish becaause I don't know the correct name for "`" (backticks)

On SQL dump files I need extract blocks on database dumps, and all database start with code below and his sql dump end with next block for other database,

--
-- Current Database: `current_database`
--


// some sql code

--
-- Current Database: `next_database`
--

for d in $(cat file_with_databasesnames.txt);do echo $d;LC_ALL=C sed -n '/^-- Current Database: `database_name`/,/^-- Current Database: `/p' mysqldump.sql > $d.sql

On code I need pass value of $d instead database_name

I try several ways but all fails.

`${d}`
`$d`
\`$d\`

I use

bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.

Edited after close for duplicate.

Are backticks same that single quotes ? I think no.

abkrim
  • 3,512
  • 7
  • 43
  • 69
  • `d="SomeDB"; echo \\`$d\\` ` gives me \`SomeDB\`; what are you trying to do differently? – jeremysprofile Jul 02 '18 at 19:55
  • See [Bash FAQ 001](https://mywiki.wooledge.org/BashFAQ/001) for the correct way to iterate over the lines of a file. – chepner Jul 02 '18 at 19:59
  • Single quoted is not backticks @that-other-guy. I think is not a duplicate question. – abkrim Jul 02 '18 at 20:01
  • @abkrim Your posted example uses single quotes. The backticks are literal and unrelated to your problem. `sed -n '/$d/p'` shows the same issue – that other guy Jul 02 '18 at 20:04
  • Thanks for tip about iterate files @chepner – abkrim Jul 02 '18 at 20:04
  • ` this is single quote ? or this ' is single quote? @that-other-guy On my code, and on my question, specific backticks (inlcude on first question before editing, because I don't now name for ` instead single quote '. but not problem. Best regards guy. – abkrim Jul 02 '18 at 20:06
  • @abkrim Here's an example of variables not showing between backticks: ``echo '`$d`'``. Here's asterisks: `echo '*$d*'`. And here it is with the word "carrot": `echo 'carrot $d carrot'`. This is not a problem with backticks, asterisks or the word "carrot", it's because it's in a single quoted string. – that other guy Jul 02 '18 at 20:10
  • Well... if try code without var `LC_ALL=C sed -n '/^-- Current Database: \`databasename\`/,/^-- Current Database: `/p' dump.sql > databasename.sql`work perfectly. But if try with @jeremysprofile all dump file failed with 0 bytes. – abkrim Jul 02 '18 at 20:11
  • Question is not echo, is code that work out on bash, but no work in bash script. `d="somedb"; echo \`$d\` `work fine, out code. But if loop code not work. – abkrim Jul 02 '18 at 20:15
  • @chepner try bash faq, but also not work.. ` while IFS= read -r line; do printf '%s\n' "$line"; LC_ALL=C sed -n '/^-- Current Database: \`$d\`/,/^-- Current Database: `/p' mysqldump.sql > $d.sql; done < "databases.txt"` – abkrim Jul 02 '18 at 20:22
  • @abkrim I've posted an example where I took the solution from the duplicate and applied it to your specific case. See if that makes it more clear. – that other guy Jul 02 '18 at 20:24
  • that said, as the answer shows, the problem *was truly* caused by single quotes, not backticks, so the close-as-duplicate was in fact correct and appropriate. – Charles Duffy Jul 02 '18 at 20:31
  • Ok, what you say. Have a nice day and thanks for the contributions. I learn more from them than from the rest. Mark it as you prefer. Best regards – abkrim Jul 02 '18 at 20:51
  • FYI `\`` is a backtick, `'` is a single quote, and `"` is a double quote. They all have **very** different functionality/uses in shell scripts - make sure to learn exactly what each does. – Ed Morton Jul 02 '18 at 21:28

1 Answers1

2

This is unrelated to your backticks, and entirely due to your single quotes. See this question for how to use variables in single quoted strings. Here's your example fixed according to that question:

for d in $(cat file_with_databasesnames.txt)
do
  echo $d;
  LC_ALL=C sed -n '/^-- Current Database: `'"$d"'`/,/^-- Current Database: `/p' mysqldump.sql > $d.sql
done

Here is an example that shows it working:

$ cat file_with_databasesnames.txt
mytarget

$ cat mysqldump.sql
-- Current Database: `users`
Some dump here
-- Current Database: `mytarget`
This is what you want
-- Current Database: `etc`
And so on

$ for d in $(cat file_with_databasesnames.txt)
do
  echo $d;
  LC_ALL=C sed -n '/^-- Current Database: `'"$d"'`/,/^-- Current Database: `/p' mysqldump.sql > $d.sql
done
mytarget

$ cat mytarget.sql
-- Current Database: `mytarget`
This is what you want
-- Current Database: `etc`

And here's a version with the general improvements of reading lines with a while read loop and showing the db name in unambiguous form:

while IFS= read -r db
do
  printf 'Processing: %q\n' "$db"
  LC_ALL=C sed -n '/^-- Current Database: `'"$db"'`/,/^-- Current Database: `/p' mysqldump.sql > "$db.sql"
done < file_with_databasesnames.txt
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks for your help. Now if there is a solution, especially for those who are not Anglophones, and who often suffer in the possibility of expressing their problem. For you it is not a problem backticks, but for me it is not the same single quotes that backticks. Nor do I believe that the question deserved its "duplicate" response. – abkrim Jul 02 '18 at 20:26
  • It might be ideal if the answer didn't violate [DontReadLinesWithFor](http://mywiki.wooledge.org/DontReadLinesWithFor) – Charles Duffy Jul 02 '18 at 20:30
  • 1
    @abkrim It's hard to tell what exactly causes a problem. This is why it is very helpful to create a [MCVE](https://stackoverflow.com/help/mcve). This helps you find what exactly the problem is. Do you see the same thing if you use `for i in "mydb"`? If you do, the problem is not with the file so don't ask about that. Do you see the same thing without the loop? If so, the problem is not with the loop so don't include it. Eventually you get down to ``sed -n '/`$d`/p'``. Do you see the same thing without the backticks? If so, `sed -n '/$d/p` is even smaller. Now the problem is much more clear. – that other guy Jul 02 '18 at 20:37