Visit Our UNIX and Linux User Community


bash Script: Issue with iterating Directory and store into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash Script: Issue with iterating Directory and store into array
# 8  
Old 08-26-2009
Try this:

Code:
#!/bin/bash

i=0

cd /tmp

for f in *; do
        arr[i++]=${f}
done

echo ${arr[*]}

exit 0

# 9  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Try this:

Code:
#!/bin/bash

i=0

cd /tmp

for f in *; do
        arr[i++]=${f}
done

echo ${arr[*]}

exit 0


when I tried to run your code I got this output.

Code:
check.sh: 9: arr[i++]=Resume_A_09_v2.pdf: not found
check.sh: 9: arr[i++]=SciTE.4115.in: not found
check.sh: 9: arr[i++]=kde-a: not found
check.sh: 9: arr[i++]=keyring-fghIJp: not found
check.sh: 9: arr[i++]=ksocket-a: not found
check.sh: 9: arr[i++]=orbit-a: not found
check.sh: 9: arr[i++]=orbit-root: not found
check.sh: 9: arr[i++]=plugtmp: not found
check.sh: 9: arr[i++]=pulse-dExDc8m05Dcj: not found
check.sh: 9: arr[i++]=seahorse-6OBRgA: not found
check.sh: 9: arr[i++]=ssh-zVGRKA3354: not found
check.sh: 9: arr[i++]=virtual-a.Zm5Csq: not found
check.sh: 11: Bad substitution

# 10  
Old 08-26-2009
Strange - what does
Code:
bash --version

show?
# 11  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Strange - what does
Code:
bash --version

show?

Yeah for me too

the output for your query is
Code:
GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

# 12  
Old 08-26-2009
Hm, mine is even older.

What happens to this?
Code:
a[0]=2
echo ${a[0]}

Might also want to try it on command line.
# 13  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Hm, mine is even older.

What happens to this?
Code:
a[0]=2
echo ${a[0]}

Might also want to try it on command line.
When I tried with terminal It works, but when I run the script, it shows the error.Smilie

Code:
new.sh: 3: a[0]=2: not found
new.sh: 4: Bad substitution


Last edited by canishk; 08-26-2009 at 04:06 AM.. Reason: adding error
# 14  
Old 08-26-2009
Ok, then try following:
Code:
echo $SHELL
which $SHELL


Previous Thread | Next Thread
Test Your Knowledge in Computers #907
Difficulty: Easy
The Unix shell command line is a sequence of ASCII text words delimited by curly braces.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck. Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification. du -sh * | sort -rh 139G Dir_1 84G Dir_2 15G Dir_3 ls -l Dir_1 drwx------ 2... (3 Replies)
Discussion started by: 308002184
3 Replies

3. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Iterating awk over a directory of files?

I need to run the same awk function over an entire directly of files. This is the awk: awk '{$(NF+1)=1}1' Is there a way that I can run this command once over all of the files, along the lines of: awk '{$(NF+1)=1}1' * so that I do not have to run this several times? My main concern is... (2 Replies)
Discussion started by: owwow14
2 Replies

5. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

6. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

7. Shell Programming and Scripting

Script to store the variable in a table or array.

Hi, I have few variable say 10 ex:- l_pc_291334_01_0_01_00.cmp l_pc_441133_50_0_02_00.cmp l_pc_441133_55_0_02_00.cmp Each variable value is coming via loop on a table. I want to create a script that stores these value to a table or array ( But one by one not all at one time as... (4 Replies)
Discussion started by: amitkumar.b2
4 Replies

8. Shell Programming and Scripting

How to store files names from a directory to an array

Hi I want to store the file names into an array. I have written like this but I am getting error. declare -A arr_Filenames ls -l *.log | set -A arr_Filenames $(awk '{print $9}') index=0 while (( $index < ${#arr_Filenames })); do Current_Filename=${arr_Filenames} ... (5 Replies)
Discussion started by: dgmm
5 Replies

9. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

Featured Tech Videos