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.