0

I am trying to run this bash program in a Centos7 machine. I have tried many different ways but all the time I get this error:

line 31: syntax error near unexpected token `done'
line 31: `done'

Line 31 belongs to the first done.

I did cat -v mybash.bash to check for weird tokens and there are none.

My script is as follows:

for mainFolder in *
do
    if [ -d "${mainFolder}" ]
    then
        cd "${mainFolder}" || exit
        echo "Entering in directory ${mainFolder}" 
        cp ../mypy.py .
        chmod +x mypy.py
        ./mypy.py
        echo "Executing mypy.py"
        sleep 1

        for subFolder in *
        do
            if [ -d "${subFolder}" ]
            then
                cd "${subFolder}" || exit
                echo "Entering in directory $subFolder in $mainFolder"
                echo "Submitting slurm file in current directory"
                sbatch *.slurm
                sleep 1
            fi
            cd ..
        done
    fi
    cd ..
done

Please help me notice what I am doing wrong.

Thanks in advanced.

  • I can't reproduce that error. Your if statements are wrong, you need spaces around [ and ]. And you should quote all your variables in case they contain spaces. – Barmar Apr 15 at 23:40
  • 1
    Paste your script into shellcheck.net to see all the other problems. – Barmar Apr 15 at 23:40
  • Which line is line 31? You only posted 27 lines. Is there more that you haven't shown? – Barmar Apr 15 at 23:41
  • yeah. This is originally a slurm submission file. I am not c/p the #SBATCH parts. – ananvodo Apr 16 at 0:12
  • 1
    You need to put the last cd .. inside the if statement. – glenn jackman Apr 16 at 0:40
0

My guess is that the slurm submission script is not there. The following checks that one and no more than one script is found.

I also moved the cd .. to happen only after a cd.

for mainFolder in *
do
    if [ -d "${mainFolder}" ]
    then
        echo "Entering in directory ${mainFolder}" 
        cd "${mainFolder}" || exit
        cp ../mypy.py .
        chmod +x mypy.py
        echo "Executing mypy.py"
        ./mypy.py
        sleep 1

        for subFolder in *
        do
            if [ -d "${subFolder}" ]
            then
                cd "${subFolder}" || exit
                echo "Entering in directory $subFolder in $mainFolder"
                scripts=$(ls *.slurm)
                nScripts=$(echo $scripts | wc -w)
                if [ $nScripts == 1 ]
                then
                    echo "Submitting $scripts"
                    sbatch $scripts
                elif [ $nScripts == 0 ]
                then
                    echo "Error: No script found"
                else
                    echo "Error: $nScripts scripts found (${scripts})"
                fi
                sleep 1
                cd ..
            fi
        done
        cd ..
    fi
done

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.