Why can’t nohup accept for loop?

Asked

Viewed 105 times

1

I have this code inside a file called test.sh When I try to execute

nohup ./teste.sh &
he makes that mistake. Someone can explain to me ?

./test.sh: 9: . /test.sh: Syntax error: Bad for loop variable

CODE


cuda="cuda.out";
cpu="cpu.out";
cudaTxt="cuda.out.txt";
cpuTxt="cpu.out.txt";
trash="/dev/null";
rm -f $cudaTxt;
rm -f $cpuTxt;

is ((i=10001;i<=1000001;i+=10000)); of echo "Cuda, $i iteration"; head -n$i formulas.txt > formulas_pre-processed_CUSTOMIZADA.txt; echo -en "$i t" >> $cudaTxt; . /$Cuda | cut -C19- >> $cudaTxt; done;

is ((i=10001;i<=1000001;i+=10000)); of echo "CPU, $i iteration"; head -n$i formulas.txt > formulas_pre-processed_CUSTOMIZADA.txt; echo -en "$i t" >> $cpuTxt; . /$cpu | cut -C19- >> $cpuTxt; done;

echo "Over";

  • Which shell you’re using?

  • Ubuntu 16.04 , his normal shell, I don’t know if that answers your question ...

  • You tried to run without the nohup to see if it is the same problem? And to know which shell you are using, run echo $SHELL or echo $0 and see what the output is. Some shells have slightly different syntaxes and the script won’t always work the same at all. A tip is to put in the first line of the script the shell you want to use to run it (for example: #!/bin/bash to use bash: http://e-tinet.com/linux/programacao-shell-script/ )

  • Yes, I tried and ran. "echo $SHELL -> /bin/bash" 'echo $0 -> -bash"

  • @hkotsubo I put #!/bin/bash and it worked But why ?

  • @Tequelequeteque I believe that the comments in Marcelo’s answer below clarify his doubt. When vc runs the direct command (no nohup), bash runs the script (since the shell you are using is bash). But when you run with nohup, it uses some other shell than bash (and this other shell does not recognize this syntax of for). But putting #!/bin/bash at the beginning of the script, you say it should always be run by bash, and then it works.

Show 1 more comment

1 answer

1


Exchange the for ((i = 10001; i <= 1000001; i += 10000)); for

i = 1
while [ $((i += 10000)) -le 1000001 ]
do
    ...
done;

EDIT: This for loop format you used is not supported by all shell types, especially those that do not support POSIX. bash is one of them. That’s why the error message contained "Bad for loop variable".

  • 1

    if possible explain your solution

  • I tried, it worked but I didn’t understand...

  • But I used without the nohup and it worked normally... I’m not understand why of the mistake... when I use ./teste.sh & it works and when I use nohup ./teste.sh & He makes that mistake ... From what I understood of the error was that this format of for loop that I used is understood by BASH but for some reason when I use with nohup it does not understand...

  • When you’re in a nohup, the level run is another and it may be that in this level run, the shell is another.

  • I put #!/bin/bash and it worked tbm, should be exactly what you said, Thank you :D

  • 1

    @Marcelouchimura Your answer is correct regarding the POSIX method of implementing such arithmetic (makes the sum within a test as a parameter for a while), but I think there’s a little confusion in the explanation: It’s POSIX that doesn’t support loops for with definition in arithmetic blocks (( ... )). The sh fails with such a loop, the bash no. This document clarifies a great deal: https://github.com/koalaman/shellcheck/wiki/SC2039#arithmetic-for-loops

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.