Bash for ... in loop continue to previous item

时间:2015-05-24 20:30:30

标签: bash for-loop

I want to do something like this:

#!/bin/bash

for var in a b c d e f; do
    read -n1 $var
    [[ "${!var}" == "d" ]] && continue the_previous_item
    echo ${!var}
done

But:

  1. continue only accepts integers.
  2. continue (AFAIK) doesn't have a previows item option.

Is there any proper solution/workaround/hack to do this?

1 个答案:

答案 0 :(得分:1)

你不太清楚你期望的行为。这会解决您的问题吗?

for var in a b c d e f; do
    while [ 1 ]; do
        read -n1 $var
        [[ "${!var}" == "d" ]] && continue
        break
    done
    echo ${!var}
done