11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

shift 2<br />

eval "eval \"\$${Hash_config_varname_prefix}${hash}_${key} \\\"\\\$@\\\"\""<br />

}<br />

# Emulates something similar to: isset(hash[key]) or hash[key]==NULL<br />

#<br />

# Params:<br />

# 1 - hash<br />

# 2 - key<br />

# Returns:<br />

# 0 - there is such key<br />

# 1 - there is no such key<br />

function hash_is_set {<br />

eval "if [[ \"\${${Hash_config_varname_prefix}${1}_${2}-a}\" = \"a\" &&<br />

\"\${${Hash_config_varname_prefix}${1}_${2}-b}\" = \"b\" ]]; then return 1; else return 0; f<br />

}<br />

# Emulates something similar to:<br />

# foreach($hash as $key => $value) { fun($key,$value); }<br />

#<br />

# It is possible to write <strong>di</strong>fferent variations of this function.<br />

# Here we use a function call to make it as "generic" as possible.<br />

#<br />

# Params:<br />

# 1 - hash<br />

# 2 - function name<br />

function hash_foreach {<br />

local keyname oldIFS="$IFS"<br />

IFS=’ ’<br />

for i in $(eval "echo \${!${Hash_config_varname_prefix}${1}_*}"); do<br />

keyname=$(eval "echo \${i##${Hash_config_varname_prefix}${1}_}")<br />

eval "$2 $keyname \"\$$i\""<br />

done<br />

IFS="$oldIFS"<br />

}<br />

# NOTE: In lines 103 and 116, ampersand changed.<br />

# But, it doesn’t matter, because these are comment lines anyhow.<br />

Ecco un script che utilizza la precedente libreria.<br />

Esempio A-22. Colorare del testo con le funzioni <strong>di</strong> hash<br />

#!/bin/bash<br />

# hash-example.sh: Colorizing text.<br />

# Author: Mariusz Gniazdowski <br />

. Hash.lib # Load the library of functions.<br />

hash_set colors red "\033[0;31m"<br />

659

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!