So, this is strange. When I manually run commands it all works fine, but when I run a script with identical commands, I get strange error output.

This is what I run:

lxc list;
for elem in {001..005}; do lxc stop ubuntu-"$elem"; done;
for elem in {001..005}; do lxc start ubuntu-"$elem"; done;
lxc list;

When I do that manually, all works with no error and my command have an actual effect. LXD machines are actually getting stopped and started again.

However, if I just write this as a script:

lxc list;
for elem in {001..005}; do lxc stop ubuntu-"$elem"; done;
for elem in {001..005}; do lxc start ubuntu-"$elem"; done;
lxc list;

I get errors (very uninformative ones, I guess about not finding machines to run command against).

error: not found
error: not found

For both loop commands.

I'm new here so please advise.

  • 2
    This looks like you've got some difference in environmental variables or their values between your login shell, and the /bin/bash that runs the script. Can you run printenv in a script and see if that matches printenv output from the interactive (login) shell? – Bruce Ediger Jul 22 '15 at 17:56
  • How exactly are you executing the script? – steeldriver Jul 22 '15 at 17:57
  • @BruceEdiger I will try and I presume a diff of both outputs should help? steeldriver sh script.sh – Anthony Jul 22 '15 at 18:20
  • 1
    @BruceEdiger I did output both script and login shell printenv and vimdiffed those - differences are vast. What can or what should I do about that? – Anthony Jul 22 '15 at 18:27
  • Can you run the script with sh -x script and paste the output? – ott-- Jul 22 '15 at 18:28

{001..005} is a bash feature but you are running your script with sh and on Ubuntu sh is dash not bash.

So don't run your script with sh script.sh but with bash script.sh or even simply ./script.sh since you have #!/bin/bash at the beginning of your script.

  • You were correct. Running with bash was fine. Obviously sh on Ubuntu 15.04 links to dash which does not support a 'for loop'; not the same way bash does. However, running ./ did not work: asked for permission, but with sudo it also just outputs "command not found". Why is that so? – Anthony Jul 22 '15 at 21:32
  • You need to give execution permission to your script, typically with chmod +x script.sh to run it with ./script.sh. I am not sure what happens with sudo in this case. – MrAfs Jul 22 '15 at 21:45

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.