Reputation: 23
I'm trying to save a list of variables modified by my script for later reloading by using set.
initialVars="`set -o posix ; set`"
source script
saveTime="$(date +"%Y-%m-%d %T")" #first time declaring this var
saveVars="`grep -vFe "$initialVars" <<<"$(set -o posix ; set)"| grep -v ^initialVars=`"
echo $saveVars > snapshot.sav
unset saveVars
This seems to work fine at first glance, but I notice snapshot.sav does not contain a lot of variables that most assuredly have been modified by the script, yet haven't even been initialized before I set initialVars. For example it does not include saveTime.
I checked the contents of initialVars, the variables that haven't been initialized yet aren't there, but ARE present if I look at (set -o posix ; set) before grepping.
Is there something wrong with the greps? I'm fairly new to bash so I might not be seeing something.
Sorry if my question is a bit convoluted, I don't have much experience with programming troubleshooting yet.
Upvotes: 1
Views: 139
Reputation: 781751
Add the -x
option so it matches whole lines:
saveVars="`grep -vxFe "$initialVars" <<<"$(set -o posix ; set)"| grep -v ^initialVars=`"
There's also no need for the saveVars
variable. You can just do:
grep -vxFe "$initialVars" <<<"$(set -o posix ; set)"| grep -v ^initialVars= > snapshot.sav
Upvotes: 1