for k in {794..800}
if [  -f    "vesta_$k.xyz"    ]
         mkdir $k
         mv vesta_$k.xyz $k/vesta.xyz
         cd $k/
         cd ..

PS vesta_$k.xyz is a sequence of files. The problem is at the first line of the IF command.

migrated from serverfault.com Feb 6 '16 at 7:42

This question came from our site for system and network administrators.

  • 1
    Put a ; at the end of if statement: if [ -f "vesta_$k.xyz" ]; – Diamant Feb 5 '16 at 9:26
  • 1
    @bangal You don't need the ; when there is a line break. – kasperd Feb 5 '16 at 10:33
  • I'm not sure this question is on-topic for Server Fault but in any case, shellcheck.net is a great resource for diagnosing shell scripting problems (such as syntax errors). – Anthony Geoghegan Feb 5 '16 at 11:07

if.. then.. fi should be fully inside for.. do.. done, you cannot cross them

for        k in {794..800}
    if     [ -f "vesta_$k.xyz" ]
           mkdir "$k"
           mv "vesta_$k.xyz" "$k/vesta.xyz"
           cd "$k" || exit 3
           echo '<<COMMANDS>>'
           cd - "$OLDPWD"
  • 2
    I would change a few other things in the script: 1: The #! line is missing. 2: Inconsistency in style for and do are on the same line, but if and then are not. Pick one style and stick with it. 3: Body of the for loop did not get indented. 3: Excessive whitespace within the [ ] construct. 4: Missing " around most expansions of $k (it is not strictly needed here, but I think it would be a good habbit to get into.) – kasperd Feb 5 '16 at 10:40
  • user1700494 and kasperd , thanks for your comments. The script is good now. I will consider your suggestions for the next ones. – D. Guedes-Sobrinho Feb 5 '16 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy