Pthread multi
[mirrors/Programs.git] / bash / bashcrawl / test.html
CommitLineData
21c4e167
H
1
2
3
4
5
6
7<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
8<html>
9<head>
10
11 <title>Using arrays in bash</title>
12
13 <meta name="description" content="Learn two ways two declare an array in bash in this Linux tip." />
14 <meta name="keywords" content="Linux,Vincent Danen,Foo,Linux Tips Newsletter" />
15
16 <style type="text/css" media="screen">
17 @import url("http://i.techrepublic.com.com/css/tr2006.css");
18 @import url("http://i.techrepublic.com.com/css/tr_ws2006.css");
19 @import url("http://news.zdnet.com/css/z/ads/hs.css");
20 @import url("http://i.techrepublic.com.com/css/trArticle.css");
21 </style>
22
23 <link rel="stylesheet" type="text/css" media="print" href="http://i.techrepublic.com.com/css/trArticlePrint.css">
24 <link REL="SHORTCUT ICON" HREF="http://techrepublic.com.com/images/favicon.ico">
25 <script type="text/javascript" SRC="http://i.techrepublic.com.com/js/articleScript.js"></script>
26 <script type="text/javascript">var dwInfo = "&oid=2100-10878_11-5820685&ptId=2100&onId=10878&sId=11&asId=5820685";</script>
27 <!-- MAC ad --><!-- NO AD TEXT: _QUERY_STRING="sp=80&pos=100" _REQ_NUM="0" --><!-- default ad --><IMG SRC="http://adlog.com.com/adlog/i/r=7007&s=501815&o=1035:&h=cn&p=2&b=9&l=en_US&site=11&pt=5100&nd=1035&pid=&cid=5820685&pp=100&e=87&rqid=00c17-ad-e5477C00C37AAE066&orh=www.google.com&ort=&oepartner=&epartner=&cpnmodule=&count=&ra=213.220.241.158&dvar=&ucat_rsi=&t=2008.01.14.02.23.42/http://i.i.com.com/cnwk.1d/Ads/common/dotclear.gif" HEIGHT="0" WIDTH="0" alt="" style="position:absolute; top:0px; left:0px" /><!-- MAC [r20071210-1033-DynamicVars:1.13.8] c13-b2b-publish-web2.cnet.com::39603120 2008.01.14.02.23.42 --><!-- MAC T 689.689.68B.68B -->
28
29</head>
30<body id="EyeBrow">
31
32<div id="goiter">
33
34 <div id="width">
35<div id="eyebrowsBanner">
36 <div id="eyebrowsInner">
37 <!-- MAC ad --><div class="rubics_netxp1_main">
38 On CHOW:
39 <a target="_top" href="http://dw.com.com/rubicsclk?ver=2&ts=2008.01.13.18.23.44&edId=87&onId=1035&ptId=5100&sId=11&appId=19&offId=948&unitId=45&poolId=1&f1=9&f2=%2d0&f3=%2d0&alg=3&&opt=0&linkPos=1&destUrl=http:%2F%2Fwww.chow.com%2Fstories%2F10785" class="xpromolink">Ten perfect grilled cheese sandwiches</a>
40 <img src="http://dw.com.com/rubicsimp/c.gif?ver=2&ts=2008.01.13.18.23.44&edId=87&onId=1035&ptId=5100&sId=11&appId=19&unitId=45&poolId=1&f1=9&f2=%2d0&f3=%2d0&alg=3&opt=0&off=948,-1" alt="" height="1" width="1"/>
41</div>
42<!-- ros [r20070521-1832-ronr-v1-13-5:1.13.5] c17-ad-rubics-ros4.cnet.com::3971599280 2008.01.13.18.23.44 --><!-- ros t 0.0.0.0.0.0.0.0 --><!-- MAC [r20071210-1033-DynamicVars:1.13.8] c13-b2b-publish-web2.cnet.com::39603120 2008.01.14.02.23.42 --><!-- MAC T 68B.68B.68D.68E -->
43 <ul class="cnbLinks">
44 <li>BNET Business Network: </li>
45 <li><a href="http://www.bnet.com">BNET</a> |</li>
46 <li><a href="http://techrepublic.com.com">TechRepublic</a> |</li>
47 <li><a href="http://www.zdnet.com">ZDNet</a></li>
48 </ul>
49 </div><!-- /eyebrowsInner -->
50 </div><!-- /eyebrowsBanner -->
51 <div id="masthead">
52 <a id="logo" href="http://techrepublic.com.com"></a>
53
54
55
56 <ul class="tool">
57 <li><a href="/5265-11518-0.html" class="blue">My Workspace</a>&nbsp;|&nbsp;<a href="http://nl.com.com/acct_mgmt.sc?brand=techrepublic" class="blue">My Newsletters</a></li>
58 <li><a href="/1320-4-55.html?path=/5213-6257-0.html" class="orange normal">Log in</a>&nbsp;|&nbsp;<a href="/1301-4-55.html?path=/5213-6257-0.html" class="orange normal">Get a free membership</a></li>
59 </ul>
60
61
62
63 </div><!-- /masthead -->
64
65
66<div id="main">
67 <ul id="headerNav">
68 <li><a href="http://techrepublic.com.com/">Home</a></li>
69 <li><a href="http://blogs.techrepublic.com.com/">Blogs</a></li>
70 <li><a href="http://downloads.techrepublic.com.com/">Downloads</a></li>
71 <li><a href="http://techrepublic.com.com/5221-6230-0.html">Forums</a></li>
72 <li><a class="on" href="http://techrepublic.com.com/2001-10875-0.html">Articles</a></li>
73 <li><a href="http://content.techrepublic.com.com/2350-1_11-0.html">Photos</a></li>
74 <li><a href="http://video.techrepublic.com.com/2434-13789_11-167680.html">Videos</a></li>
75 <li><a href="http://webcasts.techrepublic.com.com">Webcasts</a></li>
76 <li><a href="http://whitepapers.techrepublic.com.com/">White Papers</a></li>
77 <li><a href="http://software.techrepublic.com.com">Software</a></li>
78 <li><a href="http://jobs.techrepublic.com.com/">Jobs</a></li>
79
80 <li><a href="http://techrepublic.com.com/1393-6242-97.html">Store</a></li>
81
82 </ul>
83
84 <div id="topHTML">
85 <form action="http://search.techrepublic.com.com/index.php" method="get" name="tr_site_search" id="searchForm" class="articleForm">
86
87 <input id="searchInput" name="q" type="text" value="Search" onFocus="this.value=''" />
88 <select id="searchList" name="t">
89 <option value="11">in Articles</option>
90 <option value="0">all of TechRepublic</option>
91 </select>
92 <input id="searchButton" name="go" value="Search" type="submit" />
93
94 </form>
95 </div><!--/topHTML-->
96
97 <div id="content" class="clearFix">
98 <div id="leftCol">
99 <div id="contentHeader">
100 <h1>Using arrays in bash</h1>
101 <p class="byline">by <a href="http://search.techrepublic.com.com/search/Vincent Danen.html">Vincent Danen</a>&nbsp;|&nbsp;Aug 08, 2005 7:00:00 AM</p>
102
103 <p class="tags"><b>Tags:</b> <a href="http://search.techrepublic.com.com/search/Linux.html">Linux</a>, <a href="http://search.techrepublic.com.com/search/Vincent Danen.html">Vincent Danen</a>, <a href="http://search.techrepublic.com.com/search/Foo.html">Foo</a>, <a href="http://search.techrepublic.com.com/search/Linux Tips Newsletter.html">Linux Tips Newsletter</a> </p>
104
105
106
107
108
109
110
111
112
113 <div class="interact fadeBtmLeft clearFix">
114 <div class="cmntCount">
115 <a href="#comments"><span>
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 4
135
136
137
138</span> comment(s)</a>
139 </div>
140 <ul class="toolbar">
141
142
143
144<li class="share"><a title="Email this to a friend" href="javascript:usrActionReg(this,19);">Email</a></li>
145
146<li class="save"><a title="Save this to my Workspace" href="http://techrepublic.com.com/5262-6257-0.html?link=http%3A%2F%2Farticles.techrepublic.com.com%2F5100-1035_11-5820685.html%3Fpart%3Drss%26tag%3Dfeed%26subj%3Dtr&title=Using+arrays+in+bash&path=http%3A%2F%2Farticles.techrepublic.com.com%2F5100-1035_11-5820685.html%3Fpart%3Drss%26tag%3Dfeed%26subj%3Dtr">Save</a></li>
147 <li class="print"><a title="Printer-friendly version" href="javascript:usrActionReg(this,20);">Print</a></li>
148 <li class="digg"><a href="http://digg.com/submit?phase=2&url=http://articles.techrepublic.com.com/5100-1035_11-5820685.html&title=Using+arrays+in+bash&bodytext=Learn+two+ways+two+declare+an+array+in+bash+in+this+Linux+tip.&topic=tech_news" class="last">Digg This</a></li>
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 <li class="recommend"><a href="http://articles.techrepublic.com.com/1320-4-55.html?path=http://articles.techrepublic.com.com/5100-10878_11-5820685.html" TITLE="Click to recommend" class="last"></a></li>
166
167 <li class="vote"><div id="votecount" class="votecount" TITLE="How many people recommended this item">1 </div></li>
168
169<script language="JavaScript">
170 var http = createRequestObject();
171 var existing_votes = '';
172
173 function createRequestObject()
174 {
175 var ro;
176 var browser = navigator.appName;
177 if(browser == "Microsoft Internet Explorer"){
178 ro = new ActiveXObject("Microsoft.XMLHTTP");
179 }else{
180 ro = new XMLHttpRequest();
181 }
182 return ro;
183 }
184
185 function Set_Cookie(name, value, expires, path)
186 {
187 // set time, it's in milliseconds
188 var today = new Date();
189 today.setTime( today.getTime() );
190
191 expires = expires * 1000 * 60 * 60 * 24;
192 var expires_date = new Date( today.getTime() + (expires) );
193
194 document.cookie = name + "=" + escape( value ) + ";expires=" + expires_date.toGMTString() + ";path=" + path;
195
196 }
197
198 function sendVote()
199 {
200 http.open('get', '');
201 http.onreadystatechange = handleResponse;
202 http.send(null);
203 }
204
205 function handleResponse()
206 {
207 if(http.readyState == 4)
208 {
209 var response = http.responseText;
210 var temp_array = new Array();
211 temp_array = response.split('~');
212 var ranking = temp_array[0];
213
214 if (ranking > 0) {
215 ranking = ranking;
216 }
217
218 // Replace with ajax magic
219 document.getElementById('votecount').innerHTML = ranking;
220
221 // Remove the thumbs
222 var voter = document.getElementById('recommend');
223 voter.className = "thanks";
224 voter.innerHTML = "";
225
226 Set_Cookie('service',(existing_votes + '~85140'),30,'/');
227 }
228 }
229</script>
230
231
232 </ul><!-- /toolbar -->
233 </div><!-- /interact -->
234 </div><!-- /contentHeader -->
235
236 <div id="articleText">
237 <p class="takeaway"><b>Takeaway:</b> Learn two ways two declare an array in bash in this Linux tip.</p>
238
239 <div class="related">
240 <div class="relatedBg">
241 <!--Medusa Related Content-->
242 <script type="text/javascript" src="http://service.techrepublic.com.com/wi?spot=related-js&format=text&count=5&null=true&id=TR.TA.1.5820685&referer=http%3A%2F%2Fwww.google.com%2Fsearch%3Fhl%3Dcs%26client%3Diceweasel-a%26rls%3Dorg.debian%253Acs-CZ%253Aunofficial%26q%3Dbash%2Barray%26btnG%3DHledat%26lr%3D"></script>
243 <script type="text/javascript">getrelatedContent()</script>
244 <!--/Medusa Related Content-->
245 </div><!--relatedBg-->
246 </div><!-- /related -->
247
248 <p>Assigning variables in bash is easily done and extremely\r
249useful, but like other programming languages, bash can also use arrays. This is\r
250particularly handy when you want to read the contents of a file into an array or\r
251simply keep your scripts more organized and logical.</p>\r
252\r
253<p>There are two ways of declaring an array:</p>\r
254\r
255<pre>&lt;code&gt;<br>declare -a FOO<br>&lt;/code&gt;</pre>\r
256\r
257<p>This creates an empty array called FOO. You can also declare\r
258an array by assigning values to it:</p>\r
259\r
260<pre>&lt;code&gt;<br>FOO[2] = 'bar'<br>&lt;/codE&gt;</pre>\r
261\r
262<p>This assigns the third element of the array to the value\r
263'bar'. In this instance, FOO[0] and FOO[1] are also\r
264created, but their values are empty.</p>\r
265\r
266<p>To populate an array, use:</p>\r
267\r
268<pre>&lt;code&gt;<br>FOO=( bar string 'some text' )<br>&lt;/code&gt;</pre>\r
269\r
270<p>This assigns the first element (FOO[0])\r
271to 'bar', the second (FOO[1]) to 'string' and the final element (FOO[3]) to\r
272'some text'. Notice that the array elements are separated by a blank space, so\r
273if a value contains white spaces it must be quoted.</p>\r
274\r
275<p>To use an array, it is referred to as $FOO[2]\r
276but it also needs to be surrounded in curly braces, otherwise bash will not\r
277expand it correctly:</p>\r
278\r
279<pre>&lt;code&gt;<br>$ echo {$FOO[2]}<br>some text<br>&lt;/code&gt;</pre>\r
280\r
281<p>To loop through an array, you can use a piece of shell code\r
282like the following:</p>\r
283\r
284<pre>&lt;code&gt;<br>\r
285#!/bin/sh</pre><pre>FOO=( bar string 'some text')<br>foonum=${#FOO}<br>\r
286