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.

}<br />

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

s1_val=${!1} # in<strong>di</strong>rect variable expansion<br />

s2_val=${!2}<br />

eval "$1"=\’"${s1_val}${s2_val}"\’<br />

# ==> eval $1=’${s1_val}${s2_val}’ avoids problems,<br />

# ==> if one of the variables contains a single quote.<br />

#:docstring strncat:<br />

# Usage: strncat s1 s2 $n<br />

#<br />

# Line strcat, but strncat appends a maximum of n characters from the value<br />

# of variable s2. It copies fewer if the value of variabl s2 is shorter<br />

# than n characters. Echoes result on stdout.<br />

#<br />

# Example:<br />

# a=foo<br />

# b=barbaz<br />

# strncat a b 3<br />

# echo $a<br />

# => foobar<br />

#<br />

#:end docstring:<br />

###;;;autoload<br />

function strncat ()<br />

{<br />

local s1="$1"<br />

local s2="$2"<br />

local -i n="$3"<br />

local s1_val s2_val<br />

}<br />

s1_val=${!s1} # ==> in<strong>di</strong>rect variable expansion<br />

s2_val=${!s2}<br />

if [ ${#s2_val} -gt ${n} ]; then<br />

s2_val=${s2_val:0:$n} # ==> substring extraction<br />

fi<br />

eval "$s1"=\’"${s1_val}${s2_val}"\’<br />

# ==> eval $1=’${s1_val}${s2_val}’ avoids problems,<br />

# ==> if one of the variables contains a single quote.<br />

#:docstring strcmp:<br />

# Usage: strcmp $s1 $s2<br />

#<br />

# Strcmp compares its arguments and returns an integer less than, equal to,<br />

# or greater than zero, depen<strong>di</strong>ng on whether string s1 is lexicographically<br />

# less than, equal to, or greater than string s2.<br />

#:end docstring:<br />

###;;;autoload<br />

function strcmp ()<br />

640

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

Saved successfully!

Ooh no, something went wrong!