13.59.243.194 |    

Navigation

Google Advertisement

Little array library for bash.

array.inc.sh
  1. # *********************************************************
  2. # file: array.inc.sh
  3. # date: 08.10.2004
  4. # author: (c) by Marko Schulz - <info@tuxnet24.de>
  5. # description: Little array library for bash.
  6. # *********************************************************
  7. #
  8. #
  9. # SYNOPSIS
  10. #
  11. # load this file in your main script:
  12. #    test -f ./array.lib && . ./array.lib || exit 1
  13. #
  14. # to initialize the array
  15. #    declare -a array=( one two three )
  16. #    declare -a array2=( four five six )
  17. #
  18. #    echo "Display array (original) => ${array[@]}"
  19. #
  20. # add a second array to the end of first array - array_push
  21. #    array=( $( array_push ${array[@]} ${array2[@]} ) )
  22. #    echo "Display array (array_push) => ${array[@]}"
  23. #
  24. # delete last element from array - array_pop
  25. #    array=( $( array_pop ${array[@]} ) )
  26. #    echo "Display array (array_pop) => ${array[@]}"
  27. #
  28. # delete first element from array - array_shift
  29. #    array=( $( array_shift ${array[@]} ) )
  30. #    echo "Display array (array_shift) => ${array[@]}"
  31. #
  32. # merge one or more arrays together - array_merge
  33. #    array3=( $( array_merge "${array[@]}" "${array2[@]}" ) )
  34. #    echo "Display array (array_merge) => ${array3[@]}"
  35. #
  36. # check if the string 'nine' is in array 'array2' - in_array
  37. #    if in_array "nine" "$array2" ; then
  38. #        echo "The string 'nine' already exists."
  39. #    fi
  40. #
  41. # Convert the string "Fred,Barney,Wilma,Betty" into array users[]
  42. #    declare -a users=( $(explode "," "Fred,Barney,Wilma,Betty") )
  43. #    echo ${users[2]}
  44. #
  45. #
  46. # *********************************************************
  47. # add element to end of array => push
  48.  
  49. function array_push() {
  50.  
  51. local -a tmp=( $@ )
  52. echo ${tmp[@]}
  53.  
  54. }
  55.  
  56.  
  57. # *********************************************************
  58. # delete a element from end of array => pop
  59.  
  60. function array_pop() {
  61.  
  62. local -a tmp=( $@ )
  63. unset tmp[${#tmp[@]}-1]
  64. echo ${tmp[@]}
  65.  
  66. }
  67.  
  68.  
  69. # *********************************************************
  70. # delete a element from begin of array => shift
  71.  
  72. function array_shift() {
  73.  
  74. local index=0
  75. local -a tmp=( $@ )
  76.  
  77. # delete first element
  78. unset tmp[0]
  79.  
  80. # move index of array one forward
  81. for i in ${tmp[@]}; do
  82. 	tmp[$index]=$i
  83. 	let index=$index+1
  84. done
  85.  
  86. # delete last element, otherwise available double
  87. unset tmp[${#tmp[@]}-1]
  88.  
  89. echo ${tmp[@]}
  90.  
  91. }
  92.  
  93.  
  94. # *********************************************************
  95. # merge one or more arrays together => array_merge
  96.  
  97. function array_merge() {
  98.  
  99. declare -a a=( $( echo $* ) )
  100. echo ${a[@]}
  101.  
  102. }
  103.  
  104.  
  105. # *********************************************************
  106. # checks if a value exists in an array => in_array
  107.  
  108. function in_array() {
  109.  
  110. local str=$1; shift
  111. declare -a array=( $( echo $@ ) )
  112. for (( i=0; i<${#array[*]}; i++ )); do
  113.     [ "${array[$i]}" = "$str" ] && return 0
  114. done
  115. return 1
  116.  
  117. }
  118.  
  119. # *********************************************************
  120. # Split a string by a string into an arryay
  121.  
  122. function explode() {
  123.  
  124. local separator=$1; shift
  125. local string=$1
  126. local -a array
  127. local OIFS=$IFS
  128.  
  129. IFS="${separator}"
  130. read -d '' -r -a array <<< "${string}"
  131. echo -n "${array[@]}"
  132. IFS=$OIFS
  133.  
  134. }
  135.  
  136. # *********************************************************
  137. # end of this library...
Parsed in 0.003 seconds at 1115.37 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 2 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)