0

I am trying to run a simple bash script that runs through folders named subj1, subj2, etc. and executes an awk script in each folder. The problem is that the awk command is executed twice, even though it is only listed once in the script. I just started using bash and I am not sure I understand what is happening here. Any help is appreciated.

for i in `seq 1 10`;
do
  cd subj$i
  awk -f ../melt.awk subj$i_*.txt
  cd ..
done
3
  • I can't see any reason for it. However, subj$i_*.txt should be subj${i}_*.txt. Otherwise, it's looking for a variable named $i_, which doesn't exist. Commented Mar 12, 2014 at 13:28
  • 1
    Put set -x before the loop, so it will display all the commands as they're being executed. Commented Mar 12, 2014 at 13:29
  • As an aside, seq isn't a POSIX-standard command, so scripts relying on it are needlessly unportable (and also slower, since running it involves spawning a subprocess). for ((i=1; i<10; i++)) is the terse version using only bash built-in functionality. Commented Mar 12, 2014 at 13:36

1 Answer 1

3

Underscore is a valid identifier character, therefore

subj$i_*.txt

is interpreted in bash as

subj${i_}*.txt

Which is not what you want. Separate the i from the underscore:

subj${i}_*.txt

or

subj$i\_*.txt

BTW, you can probably just call

awk -f met.awk subj{1..10}_*.txt
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.