1

I am quite new at bash programming. Here is my full problem: Write a shell script that takes pairs of parameters (a filename and a number n) and for each pair will verify if the size of the file equals the number n.

Here is what i tried to do:

#!/bin/bash


for i in {$1..${@:(-2):1}..2}
    do
for j in {$2..${@: -1}..2}
    do
    if [$(stat -c%s "$i") -eq $j ]
           then
         echo $i is $j
    else
        echo $i is NOT $j
    fi
    done
done

I am trying to put into variable i the filename from each pair and in j the number n from each pair. I am then comparing the size of my file with j (the number n). I'm not sure what i;m doing wrong, i did some researsh and tested parts of my code to see if it works but something is wrong with my for statement because it kind of prints the whole . Output:

[: -eq: unary operator expected
{c.s..c.s..2} is NOT {12..12..2}

Thank you.

dsanatomy
  • 533
  • 4
  • 14

1 Answers1

0

3.5.1 Brace Expansion:

Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.

This only applies to bash, though. In ksh and zsh this would work:

$ bash -c 'foo=2; for i in {1..$foo}; do echo $i; done'
{1..2}

$ zsh -c 'foo=2; for i in {1..$foo}; do echo $i; done'
1
2

$ ksh -c 'foo=2; for i in {1..$foo}; do echo $i; done'
1
2
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71