A typical array script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A typical array script
# 1  
Old 03-09-2011
Error A typical array script

Hi All,

I need to store the output of "find ." to an array one by one. Output of find . in my case will look like :-
Code:
.
./one
./one/a
./one/b
./one/c
./two

So my first array element should be "/one" and second one "/one/a" (need to remove "." from the output as well).

Then I need to access each element from the array. Please help me out

Thanks
Renjesh

Last edited by Franklin52; 03-09-2011 at 04:26 AM.. Reason: Please use code tags
# 2  
Old 03-09-2011
Try:

Code:
arr=($(find . ! -name "." | tr '\n' ' '))


echo ${arr[0]}
echo ${arr[1]}
.
.
.

# 3  
Old 03-09-2011
I tried but not getting proper output.
Code:
rtq1@server [/home/rtq1]
$ cat test.sh
cd /home/rtq1/ESR11.2
arr=$(find . ! -name "." | tr '\n' ' ')
echo ${arr[0]}

Code:
rtq1@server [/home/rtq1]
$ ./test.sh
./one ./one/a ./one/b ./one/c ./two ./three ./content.txt ./b ./a


When i tried to print the 1st element from array it is printing all elements ..

Last edited by Franklin52; 03-09-2011 at 04:27 AM.. Reason: Please use code tags
# 4  
Old 03-09-2011
You have missed ( )
Code:
arr=($(find . ! -name "." | tr '\n' ' '))

# 5  
Old 03-09-2011
If i put that (), it is throwing a syntax error .. Smilie
Code:
$ ./test.sh
./test.sh[2]: Syntax error at line 2 : `(' is not expected.


Last edited by Franklin52; 03-09-2011 at 04:25 AM.. Reason: Please use code tags
# 6  
Old 03-09-2011
what shell you are using?

the above should work for bash.
for ksh, try

Code:
set -A arr $(find . ! -name "." | tr '\n' ' ')

# 7  
Old 03-09-2011
You can also try this
Code:
tst_arr=`find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}`
for (( i = 0 ; i < ${#tst_arr[@]} ; i++))
do
 echo "${tst_arr[$i]}"
done


Last edited by Franklin52; 03-09-2011 at 04:25 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to compile a software for a non-typical platform?

I am quite new to compiling source codes in linux and have been running into a lot of problems in trying to do so since the platform configuration is different from most. For starters, I know that you need to enter the following commands in order to install any software manually in linux:... (2 Replies)
Discussion started by: Ice_Drake1
2 Replies

2. Shell Programming and Scripting

Typical problem in UNIX

Input file I have a file with four fields. f1,f2,f3,f4 A,1,10,00,S B,2,20,00,00,D C,3,100,00,00,G I want Output like f1|f2|f3|f4 A|1|10,00|S B|2|20,00,00|D C|3|100,00,00|G please help on this (4 Replies)
Discussion started by: bharat1211
4 Replies

3. UNIX for Dummies Questions & Answers

Typical steps to be followed while applying an application patch upgrade on linux

what are the typical steps used by system adminstrators while applying an application patch upgrade (1 Reply)
Discussion started by: ramky79
1 Replies

4. Emergency UNIX and Linux Support

Calculating total space in GB for all files with typical pattern

Hi Experts, In a particular dir, I have many files *AJAY*. How can I get total size of all such files. I tried du -hs *AJAY* but it gave me individual size of all files. All I require is summation of all. Thanks, Ajay (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

5. Shell Programming and Scripting

Guidance needed for a typical shell script with sql query

Hi , I have a txt file with contents like: 1234 2345 3456 7891 I need to write a script which takes input file as txt file..run a sql query for that number and place the output of query in another file.. select * from bus_event where acct_nbr='1234'( from input txt file) the query... (20 Replies)
Discussion started by: Rajesh Putnala
20 Replies

6. UNIX for Dummies Questions & Answers

Meaning and typical use of -3 signal in kill

Hi, What is the use of the signal -3 in kill command in unix? I read the meaning and typical use of this signal in one of the Oreilly books as below. Quit -- stop running (and dump core). Sent when you type CTRL-\. what does the CTRL-\ command do? Is it the combination of CTRL and... (6 Replies)
Discussion started by: venkatesht
6 Replies

7. Shell Programming and Scripting

typical mail script

hi i have a requirement to write a mail script which needs to be automated.There are 7 CSV files generated for 7 clients in a single day.Each file will contain one header and the name of the file follows a nomenclature like ABC_20080402_ClientID.csv.ClientID is lets say... (3 Replies)
Discussion started by: dr46014
3 Replies

8. Solaris

Typical way to disable a dameon

I want to disable some services starting automatically while system booting, for instance if i want to disable vold what i have to do ? i think some services related to a script init level shell directory,and i think as well that since solaris 10 they added a command to enable and disable services... (2 Replies)
Discussion started by: XP_2600
2 Replies
Login or Register to Ask a Question