-1

If I run this command in a shell with static values (i.e., 27..97), it works fine and gives the desired output

>ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null
somefilename_27.sometxt
somefilename_28.sometxt
somefilename_29.sometxt
..
somefilename_97.sometxt

But if I want to include variables or pass arguments to regular expression then I get the error

ls: cannot access /home/work/batch/somefilename_{27..96}.*: No such file or directory

But that’s not true because the file is present but somehow with variables the regex is not working.

>segStart="27"
>segEnd="96"
>myvar="$segStart..$segEnd"
>echo $segStart
27
>echo $segEnd
96
>echo $myvar
27..96

ls -d $WORKDIR/batch/somefilename_{$myvar}.*
"ls: cannot access /home/work/batch/somefilename_{27..96}.*: No such file or directory"
>array=($(ls -d $WORKDIR/batch/somefilename_$myvar.* 2>/dev/null))
>len=${#array[*]}
>echo $len
0

Note: I am trying to store in an array the directory names where the name is between two integers: e.g., there are 1-100 directories available with name file_1.some, file_2.some, file_3.some, … file_100.some.

If a user wants to get directories from 47 till 97, then I want to read those numbers, store them, and pass them in above ls command.

If you have any other alternative that will also help.

Abdur Rahman
  • 894
  • 1
  • 11
  • 27
Haiderali
  • 89
  • 7
  • 5
    Possible duplicate of [Brace expansion with a Bash variable - {0..$foo}](https://stackoverflow.com/questions/9337489/brace-expansion-with-a-bash-variable-0-foo) – oguz ismail Dec 13 '19 at 07:30

2 Answers2

1
  1. Brace expansion and globbing are very different from regular expressions
  2. Brace expansions happen at defined places in the shell grammar (not in quotes or after parameter expansion; see man bash)
  3. Please don’t use ls to get a list of files in an array: the following should work just fine
arr=(path{1..10}*)

Or, arr=(path*) and then you can filter by testing with [[ the ones you want to get rid of.

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
  • Thank you for your prompt reply Ben, I tried using your above command "arr=(path{1..10}*)" its not working, it is giving me only one file name and not an array. Also I shall read the man in meantime, thanks again :) – Haiderali Dec 13 '19 at 06:46
  • Do note that `$arr` is not the same as `${arr[@]}` – D. Ben Knoble Dec 13 '19 at 06:47
  • yes thank you, the arr=(path{1..10}*) with ${arr[@]} works but even though the 97-100 file is not available then also it stores those filenames in the array which is not correct. Only those file which is available that should only be captured. Maybe I have to build some code around it so that it checks Again if we pass variables in arr=(path{1..10}*) then it is still not working, same as ls. – Haiderali Dec 13 '19 at 06:59
1

You can use seq command to accomplish this:

ls -d $WORKDIR/batch/somefilename_$(seq $segStart $segEnd).*

If you start from 1 for example and want to have it with leading zero (01) you can use formated output of seq like this:

seq -f "%02.0f" $segStart $segEnd"
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31