Guest User

Untitled

a guest
Dec 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. for obj in my_list:
  2. if obj == target:
  3. break
  4. else: # note: this else is attached to the for, not the if
  5. print "nothing matched", target, "in the list"
  6.  
  7. flag=false
  8. for i in x y z; do
  9. if [ condition $i ]; then
  10. flag=true
  11. break
  12. fi
  13. done
  14. if ! $flag; then
  15. echo "nothing in the list fulfilled the condition"
  16. fi
  17.  
  18. ( for i in x y z; do
  19. [ condition $i ] && echo "Condition $i true" && exit;
  20. done ) && echo "Found a match" || echo "Didn't find a match"
  21.  
  22. for i in x y z 'end-of-loop'; do
  23. if [ condition $i ]; then
  24. # loop code goes here
  25. break
  26. fi
  27. if [ $i == 'end-of-loop' ]; then
  28. # your else code goes here
  29. fi
  30. done
  31.  
  32. #!/bin/bash
  33.  
  34. shopt -s expand_aliases
  35.  
  36. alias for='_broken=0; for'
  37. alias break='{ _broken=1; break; }'
  38. alias forelse='done; while ((_broken==0)); do _broken=1;'
  39.  
  40. for x in a b c; do
  41. [ "$x" = "$1" ] && break
  42. forelse
  43. echo "nothing matched"
  44. done
  45.  
  46. $ ./t.sh a
  47. $ ./t.sh d
  48. nothing matched
  49.  
  50. while :;
  51. do for i in x y z; do
  52. if [[ condition ]]; then
  53. # do something
  54. break 2
  55. done
  56. echo Nothing matched the condition
  57. break
  58. done
  59.  
  60. if ! $flag; then
  61. echo "nothing in the list fulfilled the condition"
  62. fi
  63.  
  64. "$flag" || echo "nothing in the list fulfilled the condition"
  65.  
  66. for i in x y z; do
  67. [ condition $i ] && break #and do stuff prior to break maybe?
  68. done || echo "nothing matched"
Add Comment
Please, Sign In to add comment