<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Tech-Recipes - Latest Comments in bash shell script declaring/creating arrays</title><link>http://tech-recipes.disqus.com/</link><description>Cookbook of Tech Tutorials</description><atom:link href="https://tech-recipes.disqus.com/bash_shell_script_declaringcreating_arrays_bourne_shell_scripting_tech_recipes/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 01 Jul 2011 13:58:14 -0000</lastBuildDate><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-240152758</link><description>&lt;p&gt;thats great but can any one know how to use array in loop like ,or know how to change number from decimle into binary  plzzzzzzzzzzzzz&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bf Hayati</dc:creator><pubDate>Fri, 01 Jul 2011 13:58:14 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-79991384</link><description>&lt;p&gt;Hi, just found this site and it's been really helpful so far.  I have a problem with assigning the output of a find command into an array.  The find command works fine on it's own:&lt;/p&gt;&lt;p&gt;find "$path" \( -type d -o -type f \) -name '[.]*' -print0&lt;/p&gt;&lt;p&gt;where $path is a string with a file system path.  This finds all files or directories in a subtree with names that begin with a dot (.svn, .DS_Store, etc).  Currently, the output is piped to xargs to delete the files it finds.  It uses the -print0 to account for space characters in $path.&lt;/p&gt;&lt;p&gt;What I would like to do is have the output redirected into an array where each array element is a string path value for a file/directory with a dot name.  The problem I am having is that there are many files the subtree that have spaces in their name and when the find command output is redirected to the array the space characters in the string cause it to be split into more than one array element.&lt;/p&gt;&lt;p&gt;For example, a string like:&lt;br&gt;web/Cappuccino/iTunes Layout/Frameworks/AppKit/Resources/.DS_Store&lt;/p&gt;&lt;p&gt;will be split at iTunes&amp;lt;space&amp;gt;Layout to create array elements&lt;/p&gt;&lt;p&gt;array[n] = web/Cappuccino/iTunes&lt;br&gt;array[n+1] = Layout/Frameworks/AppKit/Resources/.DS_Store&lt;/p&gt;&lt;p&gt;I have tried a number of different syntaxes all with the same result.  I feel like it's just a simple error in my variable quoting or something, but I am stuck.  I think it should look something like this (but this is wrong):&lt;/p&gt;&lt;p&gt;array=( `find "$path" \( -type f -or -type d \) -name '[.]*' -print0 | xargs -0 -n 1` )&lt;/p&gt;&lt;p&gt;Any suggestions?  I am really stuck on this.&lt;/p&gt;&lt;p&gt;Cheers&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Duffy</dc:creator><pubDate>Wed, 22 Sep 2010 03:19:36 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-77470859</link><description>&lt;p&gt;&lt;br&gt;#Initialize commands&lt;br&gt;init command&lt;/p&gt;&lt;p&gt;echo "Total packages found: " $(rpm -qa | grep -c "some string")&lt;/p&gt;&lt;p&gt;#Search for packages and remove them one by one&lt;br&gt;for package in $(rpm -qa | grep "some string");&lt;br&gt;do&lt;br&gt;  echo "Removing " $package&lt;br&gt;  rpm -fe $package&lt;br&gt;done&lt;/p&gt;&lt;p&gt;#Cleanup&lt;br&gt;Clean up commands&lt;/p&gt;&lt;p&gt;echo "Remaining packages: " $(rpm -qa | grep -c "some string")&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Varun Avashia</dc:creator><pubDate>Tue, 14 Sep 2010 04:18:15 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-69570935</link><description>&lt;p&gt;As I was digging through my notes I found this...&lt;/p&gt;&lt;p&gt;------------------------------------------------------&lt;br&gt;NOTES : Sample code for [ jot ] command...&lt;br&gt;------------------------------------------------------&lt;/p&gt;&lt;p&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;RANDOM NUMBERS - print three random numbers between [1 - 500]&lt;br&gt;[2007.06.08](06:26AM) -&amp;gt; [bhernandez] ~ &lt;br&gt;$ operation=-r ; reps=3 ; begin=1 ; end=500 ; jot $operation $reps $begin $end&lt;/p&gt;&lt;p&gt;15&lt;br&gt;438&lt;br&gt;164&lt;br&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;/p&gt;&lt;p&gt;SEQUENCE OF NUMBERS&lt;/p&gt;&lt;p&gt;	jot 21 -1 1.00      # prints 21 evenly spaced numbers increasing from -1 to 1.&lt;/p&gt;&lt;p&gt;	jot 20 5      # prints 20 evenly spaced numbers starting at 5&lt;/p&gt;&lt;p&gt;	my_seq=`jot 20 1`      # prints 20 evenly spaced numbers starting at 1  &lt;br&gt;	echo ${my_seq}&lt;/p&gt;&lt;p&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;# ROUTINE FROM bh_dir_samples - Example using {sed, wc, jot, and a for loop}&lt;br&gt;# FOR MORE EXAMPLES SEE  --&amp;gt;  bh_dir_samples&lt;br&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;echo&lt;br&gt;echo "( 19 )  BASH - Example using {sed, wc, jot, and a for loop}"&lt;br&gt;my_path="/tmp/state/city/farm/barn/tractor/keys/red/small"&lt;br&gt;my_str=`echo "$my_path" | sed 's/\// /g'`&lt;br&gt;my_word_count=`echo $my_str | wc -w` ;&lt;br&gt;how_many_levels_back=2&lt;br&gt;max_count=`expr $my_word_count - $how_many_levels_back`&lt;br&gt;my_seq=`jot $max_count 1`      # prints 20 evenly spaced numbers starting at 1&lt;/p&gt;&lt;p&gt;n=1&lt;br&gt;for arg in $my_str ; &lt;br&gt;do &lt;br&gt;	if [ $n -gt 0 ]&lt;br&gt;	then&lt;br&gt;		if [ $n -le $max_count ]&lt;br&gt;		then&lt;br&gt;			# echo -n "$n/$arg"&lt;br&gt;			 # echo -n "/$arg"&lt;br&gt;			 my_result="${my_result}/${arg}"&lt;br&gt;		 fi&lt;br&gt;	 fi&lt;br&gt;	 let "n++"&lt;br&gt;done&lt;/p&gt;&lt;p&gt;echo "( a )        --&amp;gt;    \$my_path = $my_path"&lt;br&gt;echo "( b )        --&amp;gt;    \$how_many_levels_back = $how_many_levels_back"&lt;br&gt;echo "( c )        --&amp;gt;    \$max_count = $max_count"&lt;br&gt;echo "( d )        --&amp;gt;    \$my_seq = `echo $my_seq`"									# ( d ) This one shows up on one line&lt;br&gt;echo "( e )        --&amp;gt;    \$my_seq = ${my_seq}"											# ( e ) This shows up on separate lines&lt;br&gt;echo "( f )        --&amp;gt;    \$my_seq = `echo $my_seq | sed 's/\n/ /g'`"		# ( f ) This one shows up on one line&lt;br&gt;echo "( g )        --&amp;gt;    \$my_result = $my_result"&lt;/p&gt;&lt;p&gt;exit&lt;br&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;# RESULTS&lt;br&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;( 19 )  BASH - Example using {sed, wc, jot, and a for loop}&lt;br&gt;( a )        --&amp;gt;    $my_path = /tmp/state/city/farm/barn/tractor/keys/red/small&lt;br&gt;( b )        --&amp;gt;    $how_many_levels_back = 2&lt;br&gt;( c )        --&amp;gt;    $max_count = 7&lt;br&gt;( d )        --&amp;gt;    $my_seq = 1 2 3 4 5 6 7&lt;br&gt;( e )        --&amp;gt;    $my_seq = 1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;( f )        --&amp;gt;    $my_seq = 1 2 3 4 5 6 7&lt;br&gt;( g )        --&amp;gt;    $my_result = /tmp/state/city/farm/barn/tractor/keys&lt;/p&gt;&lt;p&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;[2007.05.08](04:40PM) -&amp;gt; [bhernandez] ~ &lt;br&gt;$ &lt;br&gt;# +---------+---------+---------+---------+---------+---------+---------+---------+---------+&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bill Hernandez</dc:creator><pubDate>Wed, 18 Aug 2010 10:46:31 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-65893869</link><description>&lt;p&gt;How can I insert elements in an array from another text file?? I want to read numbers from another text file and insert them in an array??&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ayesha</dc:creator><pubDate>Tue, 03 Aug 2010 15:13:46 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-64789914</link><description>&lt;p&gt;did u create it.. wow.... &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ll</dc:creator><pubDate>Wed, 28 Jul 2010 00:30:08 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-61672348</link><description>&lt;p&gt;to convert a decimal number into its equivalent binary number.....using shell script:&lt;/p&gt;&lt;p&gt;created by APARNA..........&lt;/p&gt;&lt;p&gt;program&lt;/p&gt;&lt;p&gt;echo " program to convert a decimal number into its equivalent binary number......." &lt;br&gt;echo -e "enter a decimal number...." &lt;br&gt;read no &lt;br&gt;d=$no &lt;br&gt;echo "____________________________________________________________" &lt;br&gt;r=0 &lt;br&gt;b=0 &lt;br&gt;if test $d -eq 0 &lt;br&gt;then &lt;br&gt;echo "Binary of 0 is 0..." &lt;br&gt;else &lt;br&gt;   if test $d -eq 1 &lt;br&gt;   then &lt;br&gt;   echo "Binary of 1 is 1..." &lt;br&gt;   else &lt;br&gt;   i=0 &lt;br&gt;echo "d=$d" &lt;br&gt;while [ $d -ne 1 ] &lt;br&gt;do &lt;br&gt; r=`expr $d % 2` &lt;br&gt; d=`expr $d / 2` &lt;br&gt;echo "       r=$r" &lt;br&gt;echo "d=$d" &lt;br&gt;#add one by one element in an array......... &lt;br&gt;a=( "${a[@]}" "$r" ) &lt;br&gt;i=$i+1 &lt;br&gt;done &lt;br&gt;a=( "${a[@]}" "$d" ) &lt;br&gt;fi &lt;br&gt;fi &lt;br&gt;echo "___________________________________________________________" &lt;br&gt;len=${#a[*]} &lt;br&gt;echo "equivalent binary number of $no  is....." &lt;br&gt;j=-1 &lt;br&gt;l=`expr $len - 1` &lt;br&gt;while [ $l -gt $j ]; do &lt;br&gt;        printf "%2d" ${a[$l]} &lt;br&gt;        let l-- &lt;br&gt;done &lt;br&gt;printf "\n" &lt;br&gt;echo " GOOD BYE.........." &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aparnacomp</dc:creator><pubDate>Mon, 12 Jul 2010 03:11:30 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-22785847</link><description>&lt;p&gt;asdasdasd&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">asd</dc:creator><pubDate>Wed, 11 Nov 2009 18:21:22 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-8770142</link><description>&lt;p&gt;Well done man! Thanks for the tip!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">bob.experience</dc:creator><pubDate>Tue, 28 Apr 2009 07:35:28 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-8110680</link><description>&lt;p&gt;Good one&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Manav</dc:creator><pubDate>Mon, 13 Apr 2009 05:49:19 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-5647468</link><description>&lt;p&gt;Reading a line, while preseriving whitespaces seems har dto do from a  file&lt;/p&gt;&lt;p&gt;You can do it this way:&lt;br&gt;&lt;/p&gt;&lt;pre&gt;cat $file | while read line; do&lt;br&gt;		echo "$line";&lt;br&gt;done&lt;br&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Erik J</dc:creator><pubDate>Thu, 29 Jan 2009 09:00:11 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-5460018</link><description>&lt;p&gt;One way to merge two arrays:&lt;/p&gt;&lt;p&gt;#existing array&lt;br&gt;existing=(item1, item2, item3);&lt;/p&gt;&lt;p&gt;#items to merge (including an item requiring correct quoting)&lt;br&gt;merge=(item4 item5 "${item6}*");&lt;/p&gt;&lt;p&gt;count=${#existing[@]};&lt;br&gt;num_new_items=${#merge[@]};&lt;br&gt;# this loop appends items to the end of the array&lt;br&gt;for (( i=0;i&amp;lt;$num_new_items;i++)); do&lt;br&gt;    echo ${i};&lt;br&gt;    existing[$count]=${merge[${i}]};&lt;br&gt;    let count+=1;&lt;br&gt;done&lt;/p&gt;&lt;p&gt;count=${#existing[@]}&lt;br&gt;for (( i=0;i&amp;lt;$count;i++)); do&lt;br&gt;    echo ${existing[${i}]};&lt;br&gt;done&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">secoif</dc:creator><pubDate>Thu, 22 Jan 2009 07:19:51 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-4427525</link><description>&lt;p&gt;Here is another method of adding elements in the array.. It works best with korn shell&lt;/p&gt;&lt;p&gt;set -A array_example 1 2 3 4 5&lt;br&gt;i=0&lt;br&gt;echo ${array_example[$i]}&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Punit</dc:creator><pubDate>Tue, 16 Dec 2008 08:01:50 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-4246290</link><description>&lt;p&gt;Wow!!! The trick for initializing an array from a file is superb!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Barun</dc:creator><pubDate>Mon, 08 Dec 2008 05:09:04 -0000</pubDate></item><item><title>Re: bash shell script declaring/creating arrays</title><link>http://www.tech-recipes.com/rx/635/bash-shell-script-declaringcreating-arrays/#comment-2768605</link><description>&lt;p&gt;here is another method to add elements to an array&lt;/p&gt;&lt;p&gt;&lt;code&gt;&lt;br&gt;for foo in $(find -type f -iname "*.png" -printf "%fn"); do&lt;br&gt;  file[${#file[*]}]=$foo&lt;br&gt;done&lt;br&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;will create an array of all png files&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">move all array</dc:creator><pubDate>Mon, 13 Jun 2005 11:05:13 -0000</pubDate></item></channel></rss>