54.226.34.209 |    

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. #
  42. # *********************************************************
  43. # add element to end of array => push
  44.  
  45. function array_push() {
  46.  
  47. local -a tmp=( $@ )
  48. echo ${tmp[@]}
  49.  
  50. }
  51.  
  52.  
  53. # *********************************************************
  54. # delete a element from end of array => pop
  55.  
  56. function array_pop() {
  57.  
  58. local -a tmp=( $@ )
  59. unset tmp[${#tmp[@]}-1]
  60. echo ${tmp[@]}
  61.  
  62. }
  63.  
  64.  
  65. # *********************************************************
  66. # delete a element from begin of array => shift
  67.  
  68. function array_shift() {
  69.  
  70. local index=0
  71. local -a tmp=( $@ )
  72.  
  73. # delete first element
  74. unset tmp[0]
  75.  
  76. # move index of array one forward
  77. for i in ${tmp[@]}; do
  78. 	tmp[$index]=$i
  79. 	let index=$index+1
  80. done
  81.  
  82. # delete last element, otherwise available double
  83. unset tmp[${#tmp[@]}-1]
  84.  
  85. echo ${tmp[@]}
  86.  
  87. }
  88.  
  89.  
  90. # *********************************************************
  91. # merge one or more arrays together => array_merge
  92.  
  93. function array_merge() {
  94.  
  95. declare -a a=( $( echo $* ) )
  96. echo ${a[@]}
  97.  
  98. }
  99.  
  100.  
  101. # *********************************************************
  102. # checks if a value exists in an array => in_array
  103.  
  104. function in_array() {
  105.  
  106. local str=$1; shift
  107. declare -a array=( $( echo $@ ) )
  108. for (( i=0; i<${#array[*]}; i++ )); do
  109.     [ "${array[$i]}" = "$str" ] && return 0
  110. done
  111. return 1
  112.  
  113. }
  114.  
  115.  
  116. # *********************************************************
  117. # end of this library...
Parsed in 0.067 seconds at 40.37 KB/s

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

User online
There are 8 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)