Persistent variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Persistent variable
# 1  
Old 01-22-2010
Persistent variable

I found a way to make a numeric variable persistent for a script :
Code:
#!/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
Code:
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! Smilie
It modifies the script itself.
For regex and sed specialists : How could it be upgraded to use other types of variables?
# 2  
Old 01-22-2010
Quote:
Originally Posted by frans
I found a way to make a numeric variable persistent for a script :
Code:
#!/bin/bash
function Persist()    {    # 1:Expression like VARIABLE=Value (numeric)


That function definition syntax is neither fish nor fowl; it is a bash-only hybrid.

The POSIX syntax is:
Code:
Persist() { ...

The ksh syntax is:
Code:
function Persist { ...

Quote:
Code:
    local V=${1%=*}
    eval "$1"
    sed -i "s/^$V=[0-9][0-9]*/$1/" $(which $(basename $0)) || return 1
}


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.

Code:
sed ... "$0"

Quote:
And how to use it
Code:
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! Smilie
It modifies the script itself.

That is generally not good practice. It is better to use a configuration file.
Quote:
For regex and sed specialists : How could it be upgraded to use other types of variables?

Code:
sed -i "s/^$V=.[^ ]*/$1/" "$0" || return 1

# 3  
Old 01-23-2010
Quote:
Originally Posted by cfajohnson
In this script there is no need for either type or basename. $0 contains the path to the script.
Allright, i was angry if the script was called another way.
Quote:
That is generally not good practice. It is better to use a configuration file.
Yes, but just for one value ?
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

BootIP vs Persistent IP in HACMP

I have done other clusters (HP MC/Service Guard and oracle Clusters, and RHEL Cluster services), and have good idea about hacmp (a little older knowledge). However the term "Boot IP" for some reason is messing with my head. Have not done HACMP since the 4.1.2.X days. Is the Bootip the... (1 Reply)
Discussion started by: mrmurdock
1 Replies

2. Linux

Add persistent route in cent os 6.5

Hi , I have cent os 6.5 and am trying to make few routes permanent for bond: i have added two routes using below two commands : /sbin/route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.59.160.128 /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 49.44.52.90 post which route command output is... (5 Replies)
Discussion started by: omkar.jadhav
5 Replies

3. Programming

Python Request Persistent

I noticed that when attempting to download videos from the url, I receive a 403 forbidden when I get through to a certain point in my downloads. I can download a third of the videos but will error: Retrieving file 'blah-video.f4v'... Traceback (most recent call last): ... (0 Replies)
Discussion started by: metallica1973
0 Replies

4. War Stories

Why am I persistent to be WRONG!

:wall::wall::wall::wall: I am sharing some techno-geeky-work related issue, which can best be dissected here only. I am expecting more of a discussion,as serious as when two guys sharing experience over smoke and beer. The thing is, till now i have been only a student, so never cared about how... (10 Replies)
Discussion started by: animesharma
10 Replies

5. Solaris

aggr not persistent after a reboot

All, I hope someone can help me on my problem with an aggregate, as I am a Solaris noob. I tried doing this according to the official documentation from Oracle (unfortunately, as a new user to the forum, I may not post URLs...) and also googled a lot around, but have not found any solution yet.... (15 Replies)
Discussion started by: Captainquark
15 Replies

6. AIX

PowerHA, same subnet for persistent and service ip

I am new in AIX and please forgive my poor english. I know that AIX allow same subnet IPs for different interfaces, which result in multipath routing / route striping. My question is, is there any best practice for the persistent and service IP with same subnet to stay on same interface, or... (5 Replies)
Discussion started by: skeyeung
5 Replies

7. AIX

Unable to add persistent IP

i am trying to make HACMP but when i add a persistent ip, error shows unable to determine address for 'UPIDGIS1_pers' pls help me out AIX - 5.3 HACMP -5.4 thanks (2 Replies)
Discussion started by: reply.ravi
2 Replies

8. AIX

HACMP Persistent IP blacklisted

Hi, I want to use the service IP incase for any network activity rather than the persistent IP as the Persistent IP is blacklisted in our network. Is there any way to make the service ip as LPARs default IP to be used as the lpars source IP incase it pings anything or acceses any external... (6 Replies)
Discussion started by: aixromeo
6 Replies

9. UNIX for Advanced & Expert Users

Mounting a USB device with a persistent name

When I attach a USB storage device to my Solaris server, the mount point is coming up as /rmdisk/unnamed_rmdisk Is there anyway I can have this device come up as a mounted device with a predetermined mount name eg /morespace rather than unnamed_rmdisk ? (2 Replies)
Discussion started by: jimthompson
2 Replies
Login or Register to Ask a Question