2

I am quite new to shell scripting. My code should be very simple, quite a tutorial, but I cannot make it work:

case='Naca0012_pitch_V1'
for i in {0..800..10} 
do
    tec $case.$i.plt;
    cat tec.00* > $case.$i.dat;
done

The command tec generates some files that I need to concatenate.

What I obtain is:

Error opening file Naca0012_pitch_V1.{0..800..10}.plt

from the tec utility.

It seems that the code puts "{0..800..10}" instead of substituting the variable with one of the possible cases from the brace expansion.

5
  • Your code after in clause is not valid. Commented Dec 13, 2018 at 9:15
  • @Simonare Sorry, what do you mean? How can i fix it? Commented Dec 13, 2018 at 9:18
  • 2
    What is the shebang of your script, which shell are you using? You mention bash, but this is perfect valid bash. Commented Dec 13, 2018 at 9:23
  • It's perfect bash if you are on version 4.0+ Please verify your bash version with echo $BASH_VERSION Commented Dec 13, 2018 at 9:31
  • In terminal, do ls -l /bin/sh. Maybe your shell is /bin/dash ?? Commented Dec 13, 2018 at 9:31

2 Answers 2

2

You try to make use of brace expansion. Depending on the shell you are using, brace expansion is supported or not. Shells that do not support brace expansion are POSIX and . Shells that do support braces expansion are , and .

Posix complient shells (sh or dash): If you want to mimick this kind of brace expansion in sh or dash, you have to do something like

for i in `seq 0 10 800`; do
  ...
done

Disable brace expansion: While , and all support brace expansion, it is possible that it has been disabled. For and this can be done by executing the command

$ set +B
$ for i in {0..800..10}; do echo $i; done
{0..800..10} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for explanation. I was using the wrong shell.
-1

Your loop ia not correct.

If your intention is looping from 0 to let say 800. Your loop is needed to be like

for n in $(seq 1 100)
do 
    #yourcode 
done

1 Comment

Sorry but your statement is wrong, the loop is perfectly valid.

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.