DISQUS

Tech-Recipes: bash shell script declaring/creating arrays | Bourne shell scripting | Tech-Recipes

  • move all array · 4 years ago
    here is another method to add elements to an array


    for foo in $(find -type f -iname "*.png" -printf "%fn"); do
    file[${#file[*]}]=$foo
    done



    will create an array of all png files
  • Barun · 11 months ago
    Wow!!! The trick for initializing an array from a file is superb!
  • Punit · 11 months ago
    Here is another method of adding elements in the array.. It works best with korn shell

    set -A array_example 1 2 3 4 5
    i=0
    echo ${array_example[$i]}
  • secoif · 10 months ago
    One way to merge two arrays:

    #existing array
    existing=(item1, item2, item3);

    #items to merge (including an item requiring correct quoting)
    merge=(item4 item5 "${item6}*");

    count=${#existing[@]};
    num_new_items=${#merge[@]};
    # this loop appends items to the end of the array
    for (( i=0;i<$num_new_items;i++)); do
    echo ${i};
    existing[$count]=${merge[${i}]};
    let count+=1;
    done

    count=${#existing[@]}
    for (( i=0;i<$count;i++)); do
    echo ${existing[${i}]};
    done
  • Erik J · 10 months ago
    Reading a line, while preseriving whitespaces seems har dto do from a file

    You can do it this way:
    <pre>
    cat $file | while read line; do
    echo "$line";
    done
    </pre>
  • bob.experience · 7 months ago
    Well done man! Thanks for the tip!
  • Manav · 7 months ago
    Good one
  • asd · 2 weeks ago
    asdasdasd