Friday, May 18, 2018

Usage of break and continue statement in a shell script,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 1
done
echo "x:$x"
echo "End"
exit 0

You can test the script after creating the necessary files such as continue.txt and break.txt in the /tmp folder


touch /tmp/continue.txt

and the script will continue till x gets 1000000

to stop this use the tab feature in terminal and in a different tab use
touch /tmp/break.txt

The shell script will stop using the break statement

No comments:

Post a Comment