0

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?

fra-san
  • 10,205
  • 2
  • 22
  • 43
jonny b
  • 83
  • 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 '19 at 22:00
  • Try adding #!/bin/bash as the first line of your script. – Crypteya Jan 21 '19 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 '19 at 22:11

2 Answers2

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.

user1133275
  • 5,574
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.

Crypteya
  • 494