Waygood
Waygood

Reputation: 2693

bash: put list files into a variable and but size of array is 1

I am listing the files in a directory and looping through them okay, BUT I need to know how many there are too. ${#dirlist[@]} is always 1, but for loop works?

#!/bin/bash
prefix="xxx"; # as example

len=${#prefix}; # string length
dirlist=`ls ${prefix}*.text`;
qty=${#dirlist[@]};  # sizeof array is always 1
for filelist in $dirlist
do
    substring="${filelist:$len:-5}";
    echo "${substring}/${qty}";
done

I have files xxx001.text upto xxx013.text
but all I get is 001/1 002/1 003/1

Upvotes: 21

Views: 83117

Answers (5)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23794

The array syntax in bash is simple, using parentheses ( and ):

# string
var=name

# NOT array of 3 elements
# delimiter is space ' ' not ,
arr=(one,two,three) 
echo ${#arr[@]}
1

# with space
arr=(one two three)
# or ' ',
arr=(one, two, three)
echo ${#arr[@]}
3

# brace expansion works as well
# 10 elements
arr=({0..9})
echo ${#arr[@]}
10

# advanced one
curly_flags=(--{ftp,ssl,dns,http,email,fc,fmp,fr,fl,dc,domain,help});
echo ${curly_flags[@]}
--ftp --ssl --dns --http --email --fc --fmp --fr --fl --dc --domain --help
echo ${#curly_flags[@]}
12

if you want to run a command and store the output

# a string of output
arr=$(ls)
echo ${#arr[@]}
1

# wrapping with parentheses
arr=($(ls))
echo ${#arr[@]}
256

A more advanced / handy way is by using built-in bash commands mapfile or readarray and process substitution. here is is an example of using mapfile:

# read the output of ls, save it in the array name: my_arr
# -t    Remove a trailing DELIM from each line read (default newline)
mapfile -t my_arr < <(ls)
echo ${#my_arr[@]}
256

Upvotes: 0

dilshad
dilshad

Reputation: 754

Declare an array of files:

arr=(~/myDir/*)

Iterate through the array using a counter:

for ((i=0; i < ${#arr[@]}; i++)); do

  # [do something to each element of array]

  echo "${arr[$i]}"
done

Upvotes: 5

KarelSk
KarelSk

Reputation: 605

This:

dirlist=`ls ${prefix}*.text`

doesn't make an array. It only makes a string with space separated file names.

You have to do

dirlist=(`ls ${prefix}*.text`)

to make it an array.

Then $dirlist will reference only the first element, so you have to use

${dirlist[*]}

to reference all of them in the loop.

Upvotes: 45

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

dir=/tmp
file_count=`ls -B "$dir" | wc -l`
echo File count: $file_count

Upvotes: 2

Costi Ciudatu
Costi Ciudatu

Reputation: 38195

You're not creating an array unless you surround it with ( ):

dirlist=(`ls ${prefix}*.text`)

Upvotes: 3

Related Questions