How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time.

Here is a fragment of my windows10/cygwin64/bash script:

export BUPLOG=$(BackupRecords --log "$src")
robocopy  $(BackupRecords -mrbd "$src" --path "$src")  $(BackupRecords --appSwitches "$src") "$src" "$dst"  $(BackupRecords --fileSwitches "$src") >$BUPLOG 2>&1
BackupRecords --complete "$src"
echo \*\*\* ALL DONE \*\*\* >>$BUPLOG 2>&1

"BackupRecords --log" is a program I wrote to return a unique log file name using the current time.

The problem is that I get two different log files. How can I force the evaluation of BUGLOG sooner?

Thanks
Siegfried

What two log files do you get?

Show us the source for your BackupRecords program. Do you really need to invoke BackupRecords five times in a four line script?

How can you have two file names? You're calling "BackupRecords --log" just once and then use the shell variable, which is not modified in between.
Do other "BackupRecords --xxx" calls have side effects?

Even if running BackupRecords has side effects, all invocations are in subshell environments (so, except for the first invocation of BackupRecords , they shouldn't be able to affect he value of the BUPLOG variable in the invoking script). The only thing that makes sense to me is that value returned by BackupRecords --log must return a string that includes a command substitution (from the description, probably including an invocation of the date utility) that produces different results each time $BUPLOG is expanded. But, the only way to know for sure is to see the source for BackupRecords .

Yes! As I you inferred: $(BackupRecords --log) very similar to $(date +'%Y %b %d %H:%M:%ss'). If I call it too often, I get different log files (which is usually what I want).

However, in this case, I want the same log file for multiple uses within the same script. How can I evaluate $(BackupRecords --log) only once at the beginning of my script once and use it multiple times later in the script?

Thanks
Siegfried

I agree with the others in that the answer lies somewhere in your BackupRecords script...

Why don't you keep using BUPLOG all over, then?

You're missing the point. The code you have does call BackupRecords --log once at the start of your script and uses the string returned by that call throughout the rest of your script. Therefore, the string returned by that command must contain something that is re-evaluating the date every time $BUPLOG is evaluated.

If you won't show us the code for BackupRecords , it is REALLY REALLY hard for us to tell you how to fix it to do what you want.

Set BUPLOG to be readonly by doing typeset -x -r BUPLOG=$(BackupRecords --log "$src") so the script will throw an error every time it's changed...

We have seen the script. There is nothing in it that changes BUPLOG after it is initially set. As I have said before, BUPLOG must be a string something like:

BackupLog$(date +'%Y%m%d%H%M%S')

so when $BUPLOG is expanded it yields a value based on the current time; not the time when BackupRecords --log was executed at the start of the script.

RudiC:
I use BUPLOG twice and that is the problem:
Using BUPLOG twice means I call $(BackupRecords --log "$src") twice which means I get two different log file names. And remember, $(BackupRecords --log "$src") is identical to "$(date +'%Y%b%d%H%M%s')".
I get the same results with BackupRecords --log "$src" and "$(date +'%Y%b%d%H%M%s')": multiple file names. So how can I store the current date and time in an environment variable once and use the environment variable multiple times and not have it change.

Thanks
Siegfried

Expanding $BUPLOG twice DOES NOT invoke BackupRecords --log "$src" twice.

I repeat for at least the third time: Please show us the code that implements your BackupRecords utility so we can help you fix it to return an unchanging backup file pathname!

No. export BUPLOG=$(BackupRecords --log "$src") assigns or defines the variable, using or expanding it like $BUPLOG does NOT change it.

I have to second Don Cragun's request for the script/source.

Unless you show what you're doing, further help won't be possible.

RudiC:
I do use it (BUPLOG) over again and the problem is that each time I use it gets a new time and I get a new log file name. I was hoping that the value of BUPLOG would be frozen in the initial export command and I would get same value every time I use $BUPLOG but this is not the sdfsdf. Remember: all "BackupRecords.exe --log" is doing is returning the current time.
Is there anyway I can force the execution of "BackupRecords.exe --log" to happen only once during the export command?

Thanks
Siegfried

Hi siegfried,
In post #2 in this thread, I said:

In post #4 in this thread I said:

In post #6 in this thread, shamrock said:

In post #8 in this thread, I said:

Instead of showing us the source for your BackupRecords program as we have requested four times, you keep asking us to tell you how to fix code that you will not let us see,.

We want to help you. PLEASE HELP US HELP YOU! Please show us the source for BackupRecords ! If you won't show us the source for BackupRecords , we are all wasting our time trying to help you. PLEASE HELP US HELP YOU! Show us the source for BackupRecords !

I apologize for not being more response. I've been working a lot of overtime and I did not have time to post an abbreviated version of BackupRecords.

I also apologize for not writing this little test program before posting.
This works fine.

export BUPLOG="`Date "+%Y-%b-%d-%H-%M-%S"`"
echo $BUPLOG
sleep 60
echo $BUPLOG
export BUPLOG="`Date "+%Y-%b-%d-%H-%M-%S"`"
echo $BUPLOG

This produces the desired results:

./logdemo.sh
2016-Apr-15-21-53-34
2016-Apr-15-21-53-34
2016-Apr-15-21-54-34

I thought surely bash was to blame for my multiple log files but apparently not! Bash is working exactly as I need it to...

And it actually turns out that sometimes I'm not getting multiple log files. So now I need to figure out why.

Thanks for all your help.
Siegfried