SnippetVamp

Because spending time searching snippets sucks.



ALV 3 XPath 1 abap 9 ahk 1 autohotkey 6 bash 2 cli 2 clipboard 3 excel 1 file 1 file_path 1 greasemonkey 1 javascript 1 python 4 sap 1 screen 1 sql 1 ssh 1 tool 2 tunnel 1

.

Store and retrieve variables in bash (from file)

Store and retrieve variables in bash (from file)

# *** Dynamic serialization in bash ***
# The follwing example can keep track of PIDs of running background processes

# The interresting part is the loading and the storage (1. and 3.) !

# *** 1. Load PIDs ***
pidfile=/foo/bar
[[ -e $pidfile ]] && . $pidfile #Source the file !

# *** 2. Do the work ***
func1 () {
    varname="$1pid"
    mybin &
    export ${varname}=$! # Pid of last background process
}

func2 () {
    varname="$1pid"
    [[ ${!varname} ]] && kill ${!varname}} || echo "Fuck"
    unset ${varname}
}

if [[ $whatever ]]; then
    func1 d
else
    func2 d
fi


# *** 3. Store back PIDs ***
[[ -e $pidfile ]] && rm $pidfile # Clean out, only variables that are still set will be serialized
for var in "d" "t" "q"; do
    varname="${var}pid"
    if [[ ${!varname} ]]; then
        declare -p ${varname} | cut -d ' ' -f 3- >> $pidfile # Here is the magic : "declare -p" handles the quotes for you :D
    fi
done

bash

<iframe width="100%" height="848" src="http://ginkobox.fr/vamp/index.php?embed=54986909effb0" type="text/html"></iframe>

Text only - Permalink - Snippet public post date 22/12/2014

This page's Feed


SnippetVamp 1.84 by Bronco - generated in 0.025 s