0

i write a backup log script and have 2 variable in echo, but still doesn't work.

My command :

echo " DB-Size $(du -hs /backup/db/db_$(date +%F).sql " >> /backup/backup.log

In my echo statement, i have command variable, in the command variable another variable. What i need to change. For every help, i'm thankfull.

kind regards

blackbeard

beard black
  • 67
  • 2
  • 14

2 Answers2

4

have 2 variable in echo

Actually, you don't.

Variables are wrapped in curly braces:

NAME=Fred
echo ${NAME}"

What you have are command invocations. Everything inside the "$(" and ")" delimiters is executed as a separate command and the output of that command "returned" as the result of the invocation:

echo "$( pwd )" 

I think your intention is to create file path based on the current date and then feed that file path into the du command. So, step by step:

Your file path is:

/backup/db/db_$(date +%F).sql

There can't be any spaces in this generated value (%F returns YYYY-MM-DD), so we don't have to worry about quoting the file path.

"Inserting" this into the du call:

du -hs /backup/db/db_$(date +%F).sql

lastly, invoking this within the "top-level" echo command:

echo "DB-Size $(du -hs /backup/db/db_$(date +%F).sql)" >> /backup/backup.log

Note the placement of the braces - "$(" starts an invocation, ")" ends it. You're invoking two commands, so you need two closing ')'s.
Also note that there are fewer quotes - one to start the string value and another to end it. Because they're double quotes, the shell will expand all the bits inside it, including variables and command invocations - if they were single quotes, it wouldn't do so, treating the whole thing as a string literal.

Phill W.
  • 1,479
  • 7
  • 7
  • I tend to recommend double-quoting all command (and variable) substitutions, unless there's a specific reason not to. In other words, rather than "can't be any spaces in this, so we don't have to quote it", I'd say "there's no reason *not* to quote this, so I'll put double-quotes around it". (And in situations where there's a reason *not* to use quotes, that's often an indication that something is wrong with the approach, and it should be rethought.) – Gordon Davisson Feb 21 '20 at 00:57
0

echo " DB-Size "$(du -hs /backup/db/db_"$(date +%F)".sql" " >> /backup/backup.log

with adding "$" for every variable solved the problem.

beard black
  • 67
  • 2
  • 14