Results 1 to 4 of 4
Hello !
I am trying to write a script which can read the names of all folders within a given folder into separate variables. In effect, if a folder contains ...
- 07-14-2010 #1Just Joined!
- Join Date
- Jul 2010
- Location
- France
- Posts
- 1
Read a Foldername into a variable
Hello !
I am trying to write a script which can read the names of all folders within a given folder into separate variables. In effect, if a folder contains a folder "Folder1", "Folder2", "Folder3", the script would read those names into variables such as:
Variable1 = "Folder1"
Variable2 = "Folder2"
Variable3 = "Folder3"
I have tried fiddling with the find . command, but without any success. Any ideas ?
- 07-14-2010 #2
assuming you are doing bashy things, try this:
anyway variables should be assigned without whitespaces as prefix or postfix of the equality. that means, your code should be written as:Code:for i in ./*; do echo $i; done;
Code:Variable1="Folder1" Variable2="Folder2" Variable3="Folder3"
- 07-14-2010 #3Just Joined!
- Join Date
- Feb 2009
- Location
- Southport, England
- Posts
- 31
Yes, and to follow on, you can use an IF statement to check if they are directories to distinguish them from regular files, i.e.:
I'm not sure how to do arrays, which is probably a good way to do this; sorry.Code:for file in *; do if [ -d $file ]; then echo $file fi done
- 07-15-2010 #4
well, an array is simple as that:
all code shipped for free, but without any warrantiesCode:array=() i=0 for file in *; do if [ -d $file ]; then array[$i]=$file; i=`expr $i + 1` fi done echo ${array[*]}
HAPPY BASHing!


Reply With Quote