Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- arr=(1 2 3)
- syntax error at line 8: `arr=' unexpected
- set '1st element' 2 3 # setting the array
- set -- "$@" more # adding elements to the end of the array
- shift 2 # removing elements (here 2) from the beginning of the array
- printf '<%s>n' "$@" # passing all the elements of the $@ array
- # as arguments to a command
- for i do # looping over the elements of the $@ array ($1, $2...)
- printf 'Looping over "%s"n' "$i"
- done
- printf '%sn' "$1" # accessing individual element of the array.
- # up to the 9th only with the Bourne shell though
- # (only the Bourne shell), and note that you need
- # the braces (as in "${10}") past the 9th in other
- # shells.
- printf '%sn' "$# elements in the array"
- printf '%sn' "$*" # join the elements of the array with the
- # first character (byte in some implementations)
- # of $IFS (not in the Bourne shell where it's on
- # space instead regardless of the value of $IFS)
- #!/bin/sh
- # ARRAY.sh: example usage of arrays in Bourne Shell
- array_traverse()
- {
- for i in $(seq 1 $2)
- do
- current_value=$1$i
- echo $(eval echo $$current_value)
- done
- return 1
- }
- ARRAY_1=one
- ARRAY_2=two
- ARRAY_3=333
- array_traverse ARRAY_ 3
- sentence="I don't need arrays because I can use delimited strings"
- for word in $sentence
- do
- printf '%sn' "$word"
- done
- #!/bin/sh
- ## The following functions implement vectors (arrays) operations in dash:
- ## Definition of a vector <v>:
- ## v_0 - variable that stores the number of elements of the vector
- ## v_1..v_n, where n=v_0 - variables that store the values of the vector elements
- VectorAddElementNext () {
- # Vector Add Element Next
- # Adds the string contained in variable $2 in the next element position (vector length + 1) in vector $1
- local elem_value
- local vector_length
- local elem_name
- eval elem_value="$$2"
- eval vector_length=$$1_0
- if [ -z "$vector_length" ]; then
- vector_length=$((0))
- fi
- vector_length=$(( vector_length + 1 ))
- elem_name=$1_$vector_length
- eval $elem_name="$elem_value"
- eval $1_0=$vector_length
- }
- VectorAddElementDVNext () {
- # Vector Add Element Direct Value Next
- # Adds the string $2 in the next element position (vector length + 1) in vector $1
- local elem_value
- local vector_length
- local elem_name
- eval elem_value="$2"
- eval vector_length=$$1_0
- if [ -z "$vector_length" ]; then
- vector_length=$((0))
- fi
- vector_length=$(( vector_length + 1 ))
- elem_name=$1_$vector_length
- eval $elem_name="$elem_value"
- eval $1_0=$vector_length
- }
- VectorAddElement () {
- # Vector Add Element
- # Adds the string contained in the variable $3 in the position contained in $2 (variable or direct value) in the vector $1
- local elem_value
- local elem_position
- local vector_length
- local elem_name
- eval elem_value="$$3"
- elem_position=$(($2))
- eval vector_length=$$1_0
- if [ -z "$vector_length" ]; then
- vector_length=$((0))
- fi
- if [ $elem_position -ge $vector_length ]; then
- vector_length=$elem_position
- fi
- elem_name=$1_$elem_position
- eval $elem_name="$elem_value"
- if [ ! $elem_position -eq 0 ]; then
- eval $1_0=$vector_length
- fi
- }
- VectorAddElementDV () {
- # Vector Add Element
- # Adds the string $3 in the position $2 (variable or direct value) in the vector $1
- local elem_value
- local elem_position
- local vector_length
- local elem_name
- eval elem_value="$3"
- elem_position=$(($2))
- eval vector_length=$$1_0
- if [ -z "$vector_length" ]; then
- vector_length=$((0))
- fi
- if [ $elem_position -ge $vector_length ]; then
- vector_length=$elem_position
- fi
- elem_name=$1_$elem_position
- eval $elem_name="$elem_value"
- if [ ! $elem_position -eq 0 ]; then
- eval $1_0=$vector_length
- fi
- }
- VectorPrint () {
- # Vector Print
- # Prints all the elements names and values of the vector $1 on sepparate lines
- local vector_length
- vector_length=$(($1_0))
- if [ "$vector_length" = "0" ]; then
- echo "Vector "$1" is empty!"
- else
- echo "Vector "$1":"
- for i in $(seq 1 $vector_length); do
- eval echo "[$i]: \"$$1_$i\""
- ###OR: eval printf '%s\n' "[$i]: \"$$1_$i\""
- done
- fi
- }
- VectorDestroy () {
- # Vector Destroy
- # Empties all the elements values of the vector $1
- local vector_length
- vector_length=$(($1_0))
- if [ ! "$vector_length" = "0" ]; then
- for i in $(seq 1 $vector_length); do
- unset $1_$i
- done
- unset $1_0
- fi
- }
- ##################
- ### MAIN START ###
- ##################
- ## Setting vector 'params' with all the parameters received by the script:
- for i in $(seq 1 $#); do
- eval param="${$i}"
- VectorAddElementNext params param
- done
- # Printing the vector 'params':
- VectorPrint params
- read temp
- ## Setting vector 'params2' with the elements of the vector 'params' in reversed order:
- if [ -n "$params_0" ]; then
- for i in $(seq 1 $params_0); do
- count=$((params_0-i+1))
- VectorAddElement params2 count params_$i
- done
- fi
- # Printing the vector 'params2':
- VectorPrint params2
- read temp
- ## Getting the values of 'params2'`s elements and printing them:
- if [ -n "$params2_0" ]; then
- echo "Printing the elements of the vector 'params2':"
- for i in $(seq 1 $params2_0); do
- eval current_elem_value="$params2_$i"
- echo "params2_$i="$current_elem_value""
- done
- else
- echo "Vector 'params2' is empty!"
- fi
- read temp
- ## Creating a two dimensional array ('a'):
- for i in $(seq 1 10); do
- VectorAddElement a 0 i
- for j in $(seq 1 8); do
- value=$(( 8 * ( i - 1 ) + j ))
- VectorAddElementDV a_$i $j $value
- done
- done
- ## Manually printing the two dimensional array ('a'):
- echo "Printing the two-dimensional array 'a':"
- if [ -n "$a_0" ]; then
- for i in $(seq 1 $a_0); do
- eval current_vector_lenght=$a_$i_0
- if [ -n "$current_vector_lenght" ]; then
- for j in $(seq 1 $current_vector_lenght); do
- eval value="$a_$i_$j"
- printf "$value "
- done
- fi
- printf "n"
- done
- fi
- ################
- ### MAIN END ###
- ################
Add Comment
Please, Sign In to add comment