0

This question already has an answer here:

I have a script that uses wget and saves the output to a file with a name of an incrementing variable.

Counter.sh:

number=1
for i in $(cat file)
do
wget $i -S -O $number.html 2>&1
((number++))

sleep 1   
echo 'done'

done

I can run the script from the command line and it operates perfectly. However when I execute it from within another script:

Script 2:

./counter.sh

I receive the following output:

scripts/counter.sh: 5: scripts/counter.sh: number++: not found
done
scripts/counter.sh: 5: scripts/counter.sh: number++: not found
done
scripts/counter.sh: 5: scripts/counter.sh: number++: not found
done
scripts/counter.sh: 5: scripts/counter.sh: number++: not found
done

For some reason the counter ++ is not working when executed from within another script. How can I fix this?

marked as duplicate by Kusalananda bash Jan 22 at 19:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • We cannot see the shebang lines at the beginning of your scripts, since you included just parts of them, so I'm just guessing. You may incur in that error if your script is invoked in a shell that does not support that syntax (((number++))), e.g. if your default shell is dash. – fra-san Jan 21 at 22:00
  • Try adding #!/bin/bash as the first line of your script. – Crypteya Jan 21 at 22:09
  • This worked! thanks, i'm not sure why this happend though as /bin/bash is my default shell – jonny b Jan 21 at 22:11
1

it looks like your shell is trying to operate on the variable number++ rather than applying an arithmetic operation to the variable number. This is likely because the ++ syntax is not supported in your shell.

To get around this you can specify the shell that you would like the script to execute with. To do this, add

#!/bin/bash

as the first line of your script.

1

Use

#!/bin/bash

or

bash counter.sh

or make it compatible with

#!/bin/sh

Generally one should use an IDE or paste your code into https://www.shellcheck.net to avoid issues like that.

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