frans
1
I found a way to make a numeric variable persistent for a script :
#!/bin/bash
function Persist() { # 1:Expression like VARIABLE=Value (numeric)
local V=${1%=*}
eval "$1"
sed -i "s/^$V=[0-9][0-9]*/$1/" $(which $(basename $0)) || return 1
}
And how to use it
AA=12
read -p "Enter a value: " NEW_AA
# verify if consistent
[ $NEW_AA != $AA ] && Persist AA=$NEW_AA # no need to store it if it isn't modified
Done!
It modifies the script itself.
For regex and sed specialists : How could it be upgraded to use other types of variables?
That function definition syntax is neither fish nor fowl; it is a bash-only hybrid.
The POSIX syntax is:
Persist() { ...
The ksh syntax is:
function Persist { ...
That will only work with the GNU version of sed.
The 'which' command is not standard and some versions may produce unpredictable results; use 'type' instead.
There is no need for the 'basename' command in any POSIX shell.
In this script there is no need for either type or basename. $0 contains the path to the script.
sed ... "$0"
That is generally not good practice. It is better to use a configuration file.
sed -i "s/^$V=.[^ ]*/$1/" "$0" || return 1
frans
3
Allright, i was angry if the script was called another way.
Yes, but just for one value ?
sed -i "s/^$V=.[^ ]*/$1/" "$0" || return 1
[/quote]
Thank You.
With your regex, the affectation statement should be alone on the line but it's not a problem.