-
Website
http://www.tech-recipes.com/ -
Original page
http://www.tech-recipes.com/rx/910/bash-array-operations/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
davak
83 comments · 1 points
-
Web Design
3 comments · 1 points
-
danishbacker
9 comments · 1 points
-
flexinfo
11 comments · 1 points
-
Tonychelle
4 comments · 1 points
-
-
Popular Threads
-
PowerPoint 2010: How To Convert a Presentation to Video (WMV format)
5 days ago · 1 comment
-
Windows Live Mail: Automatically Spell Check All Email Before Sending
1 week ago · 2 comments
-
Firefox: How to Make Google Reader the Default RSS Reader for Subscribing to Feeds
1 week ago · 1 comment
-
Facebook: How To Get Only Status Updates on Your FB Home Page
4 weeks ago · 4 comments
-
Outlook 2010: Download Images Automatically
1 week ago · 1 comment
-
PowerPoint 2010: How To Convert a Presentation to Video (WMV format)
http://www.tech-recipes.com/bourne_shell_script...
:oops: works as long as you do not try and remove element 0. Look at just this part:
${array[@]:0:$i}and you will notice the array is rebuilt using items 0 through $i. The 0 will cause element 0 to stay in the array even if
i=0Does anyone have a better fix than this:
#remove i from array
if [ $i -ne 0 ]; then
array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )
else
array=( ${array[@]:$(($i + 1))} )
fi
#remove i from arrayunset array[$i]
array=( ${array[@]} )
function remel() {
array=( `eval echo '${'$1"[@]"'}'` )
i=$2
#remove EL from array
if [ $i -ne 0 ]; then
array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )
else
array=( ${array[@]:$(($i + 1))} )
fi
echo ${array[@]}
}
#to use it:
arr=( a b c d )
echo ${arr[@]}
echo ${arr[1]}
#to remove element 1 set the array equal to an array of the output
arr=( `remel arr 1` )
echo ${arr[@]}
echo ${arr[1]}
function remel(){
array=( $@ )
let ind=${#array[@]}-1
i=${array[$ind]}
unset array[$ind]
if [ $i -ne 0 ]; then
array=( ${array[@]:0:$i} ${array[@]:$(($i + 1))} )
else
array=( ${array[@]:$(($i + 1))} )
fi
echo ${array[@]}
}
#to use it:
arr=( a b c d )
echo ${arr[@]}
echo ${arr[1]}
#to remove element 1 set the array equal to an array of the output
arr=( `remel ${arr[@]} 1` )
echo ${arr[@]}
echo ${arr[1]}
function remel() {
array=$1
i=$2
if [ $i -ne 0 ]; then
eval "$array"="( `eval echo \$\{$array[@]:0:$i\} \$\{$array[@]:$(($i + 1))\}` )";
else
eval "$array"="( `eval echo \$\{$array[@]:$(($i + 1))\}` )";
fi
}
#to use it:
arr=( a b c d )
echo ${arr[@]}
echo ${arr[1]}
#now it actually just removes the element...
remel arr 1
echo ${arr[@]}
echo ${arr[1]}