For, Until, While Loops and If statements – Bash scripting

In this video tutorial I demonstrate syntax and behavior of running for, until, and while loops in addition to if statements in bash. This is geared toward the beginner who is learning loops and flow control in bash scripting.

 

The code used in this video is below to copy to play with yourself:

TIMER=10

while [ $TIMER -ge '8' ]
	do
	echo "dont worry you still have time"
	sleep 1
	TIMER=$(($TIMER-1))
done

echo "oh boy your almost out of time, you might want to panic"
echo "you only have $TIMER seconds left"
While Loop
MYNUMBER=0

until [ $MYNUMBER -eq '5' ]
	do
	echo "im waiting to tell you my name, be patient"
	sleep 1
	MYNUMBER=$(($MYNUMBER+1))
done

echo "My name is Kyle"
Until Loop
for RESULT in `ls /home/kyle/`
	do
	echo " I have a file or folder in my home directory called '$RESULT'"
	echo " "
done

echo "end of loop"
For Loop
if [ `ls /home/kyle | wc -l` -gt "10" ]
	then
	echo "wow you have alot of files in your home directory"
else 
	echo "you dont have too many files in your home directory"
fi
	
echo "end of IF statement"
IF Statement

Be the first to comment

Leave a Reply

Your email address will not be published.


*