Multiple Variables in Array from Existing Directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Variables in Array from Existing Directories
# 1  
Old 08-26-2010
Multiple Variables in Array from Existing Directories

I would like to extract directories from a specific place and keep them into an array of variables to run functions into it. Example,

Code:
bash-3.00$ls
adrian  bryan caren derrick

I want to keep each directory names into a variable

adrian --> document[0]
bryan --> document[1]
caren --> document[2]
derrick --> document[3]

How do I do that in a shell script?
# 2  
Old 08-26-2010
For bash you can try:
Code:
$> declare -a ARRAY
$> ARRAY=( `ls` )

# For single elements:
$> echo ${ARRAY[1]}
# For all elements:
$> echo ${ARRAY[*]}

If it is ok to just have a list instead of an array you could pipe the output of ls into a while/read loop to work on the single elements.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-26-2010
Code:
x=0;for i in adrian bryan caren derrick; do document[x]=`echo "$i"`; echo "${document[x]} --> document[$x]";let x=x+1; done
adrian --> document[0]
bryan --> document[1]
caren --> document[2]
derrick --> document[3]

# 4  
Old 08-30-2010
This is exactly what I'm looking for! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies

2. Shell Programming and Scripting

Chksum on two directories then copy if they are not identical or existing

How can I have an intelligent script that will copy from source to destination directory if the file doesnt exist there or the chksum is not match. SOURCE directory: for i in `ls` > do > echo $i > md5sum $i > echo "" > done asdasda 00039a616135792fb609d04cf27aed95 asdasda ... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

3. Shell Programming and Scripting

Finding non-existing words in a list of files in a directory and its sub-directories

Hi All, I have a list of words (these are actually a list of database table names separated by comma). Now, I want to find only the non-existing list of words in the *.java files of current directory and/or its sub-directories. Sample list of words:... (8 Replies)
Discussion started by: Bhanu Dhulipudi
8 Replies

4. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

5. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

6. UNIX for Dummies Questions & Answers

Setting up existing Directories using facls to recursively add an individual user

I've been working with Solaris/Linux for about 4 months now. Let me explain the scenario. There will be two users involved. The owner (curOwner) and the new user (newUser). The server in question is a Solaris 10 box. So curOwner runs an application that is constantly writing logs to lets say.... (2 Replies)
Discussion started by: amadont12
2 Replies

7. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

8. Shell Programming and Scripting

append newline to existing variables

firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${vesselNameList}""\n"`echo "$vesselName"` and it produces this result BUNGA TERATAI 3 5055\ JADE TRADER 143W\ MOL SPLENDOR 0307A BUNGA TERATAI 3 5055\ JADE... (1 Reply)
Discussion started by: finalight
1 Replies

9. Solaris

adding existing disks to a 3510 array

I would like to extend a logical drive on our 3510. I have four unallocated disks which I would like to use for this purpose. The 3510 supports a Sun Cluster but for now all I wish to see is a "new" disk when I run format. I am a little familiar with the telnet/ssh session on the 3510 but am... (2 Replies)
Discussion started by: malcqv
2 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question