27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

# Replace all matching wildcarded string<br />

declare −a array5=( ${array0[@]//new?/old} )<br />

echo<br />

echo "Elements in array5: ${array5[@]}"<br />

# Just when you are getting the feel for this...<br />

declare −a array6=( ${array0[@]#*new} )<br />

echo # This one might surprise you<br />

echo "Elements in array6: ${array6[@]}"<br />

declare −a array7=( ${array0[@]#new1} )<br />

echo # After array6 this should not be a surprise<br />

echo "Elements in array7: ${array7[@]}"<br />

# Which looks a lot like...<br />

declare −a array8=( ${array0[@]/new1/} )<br />

echo<br />

echo "Elements in array8: ${array8[@]}"<br />

# So what can one say about this?<br />

# The string operations are performed on<br />

#+ each of the elements in var[@] in succession.<br />

# Therefore : BASH supports string vector operations<br />

# If the result is a zero length string, that<br />

#+ element disappears in the resulting assignment.<br />

# Question, are those strings hard or soft quotes?<br />

zap='new*'<br />

declare −a array9=( ${array0[@]/$zap/} )<br />

echo<br />

echo "Elements in array9: ${array9[@]}"<br />

# Just when you thought you where still in Kansas...<br />

declare −a array10=( ${array0[@]#$zap} )<br />

echo<br />

echo "Elements in array10: ${array10[@]}"<br />

# Compare array7 with array10<br />

# Compare array8 with array9<br />

# Answer, must be soft quotes.<br />

exit 0<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

The relationship of ${array_name[@]} and ${array_name[*]} is analogous to that between $@ and $*. This<br />

powerful array notation has a number of uses.<br />

# Copying an array.<br />

array2=( "${array1[@]}" )<br />

# or<br />

array2="${array1[@]}"<br />

# Adding an element to an array.<br />

array=( "${array[@]}" "new element" )<br />

# or<br />

array[${#array[*]}]="new element"<br />

# Thanks, S.C.<br />

Chapter 26. Arrays 297

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

Saved successfully!

Ooh no, something went wrong!