Loop control from a subset
source link: https://www.codesd.com/item/loop-control-from-a-subset.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Loop control from a subset
I want to use subshells for making sure environment changes do not affect different iterations in a loop, but I'm not sure I can use loop control statements (break
, continue
) inside the subshell:
#!/bin/sh
export A=0
for i in 1 2 3; do
(
export A=$i
if [ $i -eq 2 ]; then continue ; fi
echo $i
)
done
echo $A
The value of A
outside the loop is unaffected by whatever happens inside, and that's OK. But is it allowed to use the continue
inside the subshell or should I move it outside? For the record, it works as it is written, but maybe that's an unreliable side effect.
Just add
echo "out $i"
after the closing parenthesis to see it does not work - it exits the subshell, but continues the loop.
The following works, though:
#! /bin/bash
export A=0
for i in 1 2 3; do
(
export A=$i
if [ $i -eq 2 ]; then exit 1 ; fi
echo $i
) && echo $i out # Only if the condition was not true.
done
echo $A
Related Articles
I need to determine what action to take based on the loop control variable
How to create controls from code in a custom control?
How to assign HTML content to the iframe control from asp.net codebehind?
Algorithms for selecting a uniformly random number from a subset of integers
Unable to access User Control child controls from code behind
Removing User Controls from the Parent Form
Creating Web User Controls from ascx.cs
Using a stopclock-based func as a loop control variable
How do I remove my control from the action sheet without clicking on the buttons in the iPhone?
I renamed the aspx file name but now I can not access the controls from the cs file
GREP loop control
Access Windows Control from Backgroundworker DoWork
How to prevent the control from stealing the focus?
WinForms: How do I include a user control from an external library?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK