1

The command below is being used in a simple bash script to get the temporary password in MySQL 5.7.

vzctl exec $VEID 'defaultpass=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root@localhost: //p')'

However it says the following when I run the command for testing:

sed: -e expression #1, char 19: unterminated `s' command

Can someone please explain me what I am doing wrong exactly. I am by no means an expert, but I thought this would be correct. I also tried replacing ' with ", but that didn't work. No errors then, but $defaultpass returns empty then.

Follow-up: unfortunately when I use double-quotes,as described by pii_ke, the output of the variable is empty / blank. So that's not working. Maybe something else is wrong here?

Joanne
  • 514
  • 3
  • 8
  • 30
  • Not sure which exactly character causes this, but if you want to NOT quote regex chars you have to use `sed -E` – Gnudiff Dec 15 '17 at 13:53
  • why not separate out into multiple commands instead of single complicated command? and no need to use `cat` there.. just do `grep 'string' filename` – Sundeep Dec 15 '17 at 14:52

1 Answers1

4

Since the whole third argument to vzctl command is quoted with single quotes. You should place sed commands inside double quotes:

vzctl exec $VEID 'defaultpass=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n "s/.*root@localhost: //p")'
pii_ke
  • 2,811
  • 2
  • 20
  • 30
  • Nevermind, I was wrong. The output is empty when I call the variable defaultpass. This is what i meant that I already tried it with double quotes. At first I thought it worked, but I already used the variable before (so it still remembered that input). So if I use your example; with double quotes and I call the variable the output is blank/empty...? – Joanne Dec 15 '17 at 14:08
  • 1
    Try something like `defaultpass=$(vzctl exec $VEID '…')`. Then it will set `defaultpass` in current scope. – pii_ke Dec 15 '17 at 15:19