Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for obj in my_list:
- if obj == target:
- break
- else: # note: this else is attached to the for, not the if
- print "nothing matched", target, "in the list"
- flag=false
- for i in x y z; do
- if [ condition $i ]; then
- flag=true
- break
- fi
- done
- if ! $flag; then
- echo "nothing in the list fulfilled the condition"
- fi
- ( for i in x y z; do
- [ condition $i ] && echo "Condition $i true" && exit;
- done ) && echo "Found a match" || echo "Didn't find a match"
- for i in x y z 'end-of-loop'; do
- if [ condition $i ]; then
- # loop code goes here
- break
- fi
- if [ $i == 'end-of-loop' ]; then
- # your else code goes here
- fi
- done
- #!/bin/bash
- shopt -s expand_aliases
- alias for='_broken=0; for'
- alias break='{ _broken=1; break; }'
- alias forelse='done; while ((_broken==0)); do _broken=1;'
- for x in a b c; do
- [ "$x" = "$1" ] && break
- forelse
- echo "nothing matched"
- done
- $ ./t.sh a
- $ ./t.sh d
- nothing matched
- while :;
- do for i in x y z; do
- if [[ condition ]]; then
- # do something
- break 2
- done
- echo Nothing matched the condition
- break
- done
- if ! $flag; then
- echo "nothing in the list fulfilled the condition"
- fi
- "$flag" || echo "nothing in the list fulfilled the condition"
- for i in x y z; do
- [ condition $i ] && break #and do stuff prior to break maybe?
- done || echo "nothing matched"
Add Comment
Please, Sign In to add comment