DISQUS

DISQUS Hello! Tech-Recipes is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Tech-Recipes

Cookbook of Tech Tutorials
Jump to original thread »
Author

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

Started by qdideas · 9 months ago

No excerpt available. Jump to website »

7 comments

  • 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
  • Wow!!! The trick for initializing an array from a file is superb!
  • 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]}
  • 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
  • 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>
  • Well done man! Thanks for the tip!
  • Good one

Add New Comment

Returning? Login