0

I'm creating a sh script on my raspberry for a timelapse. I've included in the script 4 command that will successively take place, each command tested and working. Now my question is: how to come back to the first command after the last one, indefinitely?

#!/bin/bash

sudo raspistill -w 1024 -h 768 -o /home/pi/timelapse/a%04d.jpg -t 600000 -tl 30000
sudo kill $(ps ax | grep 'timelapse' | awk '{print $1}')
sudo avconv -r 10 -i /home/pi/timelapse/a%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 timelaps$
sudo rm /home/pi/timelapse/*.jpg

So after sudo rm /home/pi/timelapse/*.jpg I want to go back to the first command.

Would you have any idea?

thanks.

6
  • man yes or while but that would be an infinite loop, what is the purpose of all this? Commented Feb 9, 2016 at 12:32
  • 1
    Never pipe grep to awk. awk '/timelapse/{print $1} is the same as grep timelapse | ... Commented Feb 9, 2016 at 12:35
  • Possible duplicate of bash loop with GNU less that refreshes every 2 minutes [that's an example] Commented Feb 9, 2016 at 12:35
  • 2
    If you're going to run every command in the script with sudo, it's probably simpler to just run the script itself with sudo. Commented Feb 9, 2016 at 12:36
  • @WilliamPursell I wouldn't say never. for example using the -o on grep is far easier than acquiring the same output in awk Commented Feb 9, 2016 at 13:01

1 Answer 1

3

You can use a loop:

#!/bin/sh

while true; do 
    ...
done

or, re-invoke the script:

#!/bin/sh

...

exec $0 "$@"

Frankly, either one of these seems risky in your case since you're doing no error checking at all, and you run the risk of entering a relatively fast loop of commands continuously failing. At the very least, you should pause for a bit by using while sleep 1; instead of while true;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.