1

I got one error in the first expr

nmin =$(expr ($nr-1)*31)
nmax =$(expr ($nr*31)-1)    
array=`ls *d03_*`   # specify files to be subsetted
for name in ${array[@]:nmin:30}

After reading the documentation seems like one space or really minor thing is creating the error

Also i don t know is the slicing in for is correct .

gis20
  • 1,024
  • 2
  • 15
  • 33

2 Answers2

3

You have several errors in your code. First, you cannot put whitespace before or after the = in an assignment statement.

nmin=$(expr ($nr-1)*31)
nmax=$(expr ($nr*31)-1)   

Second, expr is not needed for arithmetic; the shell can do that itself.

nmin=$(( ($nr-1)*31 ))
nmax=$(( ($nr*31)-1 ))

Third, the proper way to assign an array is with parentheses

array=( `ls *d03_*` ) 

Fourth, it's never appropriate to use ls like this. Just expand the glob directly into the array:

array=( *d03_* )

Your for loop is actually almost correct; you should, however, quote the expansion so that any whitespace in each array element is preserved.

for name in "${array[@]:nmin:30}"; do

which iterates over ${array[nmin]} through ${array[nmin+29]}.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

An array definition needs round parenthesis.

This assigns a string to the variable x:

x=`echo $? $$`
echo "$x"

This assigns two values to an array:

x=(`echo $? $$`)
echo "${x[@]}"
ceving
  • 21,900
  • 13
  • 104
  • 178
  • The `echo` in the string is most unnecessary; `x=(1 2)`. – chepner Feb 09 '16 at 15:29
  • @chepner It is just the most simple place holder for any kind of sub command. A sub command is necessary, because it is part of the question. – ceving Feb 09 '16 at 15:33
  • It's almost never correct to run a command and assume its output is suitable for splitting into an array; it's *never* correct to do so with `ls`. – chepner Feb 09 '16 at 15:37
  • No, it's *pointless* to use `echo`. You are forking a new process just to produce values you could have specified literally. – chepner Feb 09 '16 at 15:39
  • ...and if your goal is to read a list of values into an array, the more correct replacement for `x=( $(...) )` is `read -r -a x < <(...)`, which won't expand globs (look at what happens when you have `*` as one of your outputs). But for output from `ls`, none of that is correct (because `ls` is inherently the wrong tool for the job). – Charles Duffy Feb 09 '16 at 15:48
  • @ceving `x=( $? $$ )`. – chepner Feb 09 '16 at 16:32
  • @CharlesDuffy No my goal was not to read a list of values. My goal was to point out that the parenthesis are missing. If you take the time to read my answer, you will find this right in the first sentence. It is the sixth word in the first sentence. – ceving Feb 10 '16 at 11:50
  • @ceving, yes, you're showing declaration of an array, but you're simultaneously *populating* that array via string-splitting and glob expansion, which -- as chepner and I have been trying to convey -- is the wrong way to do it. Using `read -a` will both create *and* populate an array, without all the bugs. – Charles Duffy Feb 10 '16 at 15:44
  • @ceving, ...`x=( "$?" "$$" )` would be perfectly fine code for demonstrating how to declare and populate an array; `x=( $(echo $? $$) )`, or any syntactical equivalent, is not, because it shows -- in what is by its context as an "answer" purportedly a demonstration of best practices -- a potentially hazardous antipattern. – Charles Duffy Feb 10 '16 at 15:47