Unix Script control flow


/ Published in: Bash
Save to your folder(s)

you could divide your job into functions
Here, procedure_1 is the first part of the job, where there isn't restart, procedure_2 must be executed if procedure_1 was done and the control logic is at the end, working as an outer loop.


Copy this code and paste it in your HTML
  1. #!/bin/sh
  2.  
  3. # The file which will save the current position in the job
  4. BREAKPOINT=/mydir/breakpoint
  5. MAXPROCEDURE=3
  6. #
  7. # All the global variables should be declared here
  8. #
  9.  
  10. save_break_point()
  11. {
  12. echo $BP >$BREAKPOINT
  13. }
  14.  
  15. procedure_1()
  16. {
  17. # First part of the job
  18. echo procedure 1
  19. }
  20.  
  21. procedure_2()
  22. {
  23. # Second part of the job
  24. echo procedure 2
  25. }
  26.  
  27. procedure_3()
  28. {
  29. # Third part of the job
  30. echo procedure 3
  31. }
  32.  
  33. #
  34. # Initial state
  35. [ -e $BREAKPOINT ] && BP=`cat $BREAKPOINT | sed 's/^\([0-9]*\).*$/\1/'`
  36. [ "$BP" = "" ] && BP=1
  37.  
  38. # Process each function based on the breakpoint
  39.  
  40. while [ $BP -le $MAXPROCEDURE ]
  41. do
  42. procedure_$BP
  43. save_break_point
  44. BP=`expr $BP + 1`
  45. done
  46. [ -e $BREAKPOINT ] && rm -f $BREAKPOINT

URL: http://www.linuxquestions.org/questions/programming-9/bash-goto-384407/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.