1

I have this script:

DB_ARRAY=(NAME USER PASSWD SERVER)

for i in ${DB_ARRAY[@]}; do
    VAR="DB_$i"
    declare $VAR=$(sed -rn "/_${VAR}_/ { s/.*'_${VAR}_', '(\w+)'\);/\1/p }" ${PRESTASHOP_PATH}/config/settings.inc.php | tail -1)
done

mysqldump -u"${DB_USER}" -p"${DB_PASSWD}"-h"${DB_SERVER}" "${DB_NAME}" > ${DATABASE_PATH}

I'm basically using it for getting database variables from Prestashop configuration to do a database dump. It works with Linux.

However today I got this when trying it on a new system:

sed: 1: "/_DB_NAME_/ { s/.*'_DB_ ...": bad flag in substitute command: '}'
sed: 1: "/_DB_USER_/ { s/.*'_DB_ ...": bad flag in substitute command: '}'
sed: 1: "/_DB_PASSWD_/ { s/.*'_D ...": bad flag in substitute command: '}'
sed: 1: "/_DB_SERVER_/ { s/.*'_D ...": bad flag in substitute command: '}'

It turns out, the system I was trying it on is FreeBSD.

  1. Is it possible to make it FreeBSD compatible? How?
  2. What is the difference between FreeBSD sed and Linux sed that is responsible for this problem?

Added: This is example settings.inc.php file, I'm parsing with sed:

define('_DB_SERVER_', 'mariadb');
define('_DB_NAME_', 'mydatabase');
define('_DB_USER_', 'prestashop');
define('_DB_PASSWD_', 'prestashop');
define('_DB_PREFIX_', 'ps_');
define('_MYSQL_ENGINE_', 'InnoDB');
define('_PS_CACHING_SYSTEM_', 'CacheFs');
define('_PS_CACHE_ENABLED_', '0');
define('_COOKIE_KEY_', 'xyz');
define('_COOKIE_IV_', 'xyz');
define('_PS_CREATION_DATE_', '2015-04-30');
define('_PS_VERSION_', '1.6.1.0');
define('_RIJNDAEL_KEY_', 'xyz');
define('_RIJNDAEL_IV_', 'xyz');
define('_PS_DIRECTORY_', '/../../');
Łukasz Zaroda
  • 869
  • 2
  • 19
  • 55
  • What has your researched revealed so far? For example, did you create a MCVE? Did you check [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html)? – that other guy Sep 10 '18 at 23:12
  • Have you tried this: https://stackoverflow.com/questions/17534840/sed-throws-bad-flag-in-substitute-command – Ashutosh Sep 10 '18 at 23:16
  • Shouldn't there be a space before `-h` in `mysqldump`? Sure `PRESTASHOP_PATH` contains no spaces and special characters and doesn't need to be escaped in `"` ? – KamilCuk Sep 10 '18 at 23:20
  • 2
    Just add a semi-colon: `{ s/.*'_${VAR}_', '(\w+)'\);/\1/p; }` – William Pursell Sep 10 '18 at 23:30
  • Although in this case it's probably easier to just drop the `{}` – William Pursell Sep 10 '18 at 23:30
  • @WilliamPursell Hm, thats interesting, it made errors go away, however vars are empty, it's like this `sed` line doesn't work on FreeBSD, cannot find strings I'm looking for, and yet it works on Linux (with `;` and without it). The difference between sed behavior on Linux and FreeBSD has to go deeper than that :/ . – Łukasz Zaroda Sep 11 '18 at 23:00
  • `\w` isn't guaranteed by the POSIX standard to work in BRE *or* ERE, so relying on it (or `\s` or so forth) in portable code is a no-no. I'm actually surprised `sed -r` works for you at all -- some BSD platforms require it to be `sed -E` to enable ERE extensions. – Charles Duffy Sep 11 '18 at 23:13
  • BTW, there are some other quoting bugs in this code -- consider running it through http://shellcheck.net/ and fixing what that finds. Also, note that all-caps names are used for variables meaningful to the shell itself, whereas names with at least one lowercase character are reserved for application use and guaranteed not to conflict with shell behavior; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that shell and environment variables share a namespace. – Charles Duffy Sep 11 '18 at 23:17
  • ...on point for what is and isn't guaranteed to be available syntax for POSIX BRE (which `sed` uses by default) and ERE (which `-r` or `-E` enables on some extended versions of `sed`) -- see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html, and http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html as a formal description of the functionality `sed` is guaranteed to have available across platforms. – Charles Duffy Sep 11 '18 at 23:20
  • Note also how [BashFAQ #21](https://mywiki.wooledge.org/BashFAQ/021) often advises `awk` as a more appropriate tool than `sed` for string substitution -- its use in `gsub_literal` being, perhaps, particularly pertinent. – Charles Duffy Sep 11 '18 at 23:22
  • Thanks for all tips, I will certainly read all that. That's first time I found myself outside of the Linux territory, so it helps a lot :) . – Łukasz Zaroda Sep 11 '18 at 23:24

0 Answers0