New Line help needed

I am doing a simple SHOW DATABASES query:

#!/bin/bash
echo `mysql -e "SHOW DATABASES;"`

It produces this:

Database information_schema mysql test

There are 2 things I want to do, but failing at.

  1. Exclude the header "Database information_schema"
  2. add a new line between each database "\n"

I'm running into walls for both of them.

I've tried different ways to use tail +2 (echo `mysql -e "SHOW DATABASES;"` | tail +2) - but to no avail. And the new line is elusive.

Any thoughts?

Hi.

Why are you using echo at all?

That will only help to supress the newlines.

If you have to perhaps use the -e option

man echo

(-e is only available on Linux!)

What's the output from 'mysql -e "SHOW DATABASES;"' on it's own look like?
Without seeing the raw output we're working with it's hard to be sure, but I would guess you want something like:

for database in `mysql -e "SHOW DATABASES;" | tail +2`
do
  echo $database
done
#!/bin/sh
for dbName in `mysqlshow -uroot -pPassWord | awk '{print $2}'`
do
echo "$dbName" | egrep -v 'mysql|test|Databases|information_schema';
done
NL='
'
db=$( mysql -e "SHOW DATABASES;" )
db=${db#*"$NL"}
printf "%s\n" "$db"

Assuming the output is like this:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
mysql -e "SHOW DATABASES;"|grep -o '\w*'|tail -n +3

will produce:

mysql
test

Not so. It is part of bash, which may be found on any system.

However, echo is deprecated; use printf instead.

My mistake, but the main point was what echo at all had to do with it?

It's non standard, all the same

echo