Seabrook Project 5: traffic light Here is a shell program that creates a bi-stab
ID: 3705928 • Letter: S
Question
Seabrook Project 5: traffic light
Here is a shell program that creates a bi-stable process.
===================================
#!/bin/bash
#blinker
#creates a bi-stable process that displays ON and OFF
touch .running
clear
while [ -f .running ]
do
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 5 ]
then
break
fi
echo "ON"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 2 ]
then
break
fi
echo "OFF"
sleep 1
done
done
===================================
Here is an example of its second-by-second output:
===================================
ON
ON
ON
ON
ON
OFF
OFF
ON
ON
ON
..
..
===================================
Modify this script to create trafficlight, a tri-stable
process that includes a 4-second green period, a 2-second
yellow period and a 4-second red period, and when run creates
the following output:
===================================
GREEN
GREEN
GREEN
GREEN
YELLOW
YELLOW
RED
RED
RED
RED
GREEN
GREEN
.....
.....
===================================
When your trafficlight script runs correctly, create another
script called turnlight, a 4-stable process that does everything
your trafficlight script does plus displays a 2-second left-turn
arrow just before entering the green period. You may employ any
output design you wish. Here's an example:
===================================
GREEN
GREEN
GREEN
GREEN
YELLOW
YELLOW
RED
RED
RED
RED
<-
<-
GREEN
GREEN
.....
.....
===================================
Note: if you run your project in the background you can remove
the .running file to make it stop.
Document your project carefully and be prepared to
demonstrate it in the lab. Online students submit
your trafficlight and turnlight scripts and output
examples.
Rev: 2018-3-26
Explanation / Answer
The core logic of the program lies in the following code:
count = 0
do
count=$((count+1))
if [ $count -gt 5 ]
then
break
fi
echo "ON"
sleep 1
done
What the above code does is following.
1. Initialize count to value 0
2. Next it increment count by 1
3. Next it checks if count is greater than 0. If true the loop is broken
4. Else it print 'ON' and the loop continues after waiting for 1 second (step 2 continues again)
Conidering the above pattern, code to print the RED, YELLOW and GREEN light would be:
#!/bin/bash
#blinker
#creates a bi-stable process that displays ON and OFF
touch .running
clear
while [ -f .running ]
do
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 4 ]
then
break
fi
echo "GREEN"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 2 ]
then
break
fi
echo "YELLOW"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 4 ]
then
break
fi
echo "RED"
sleep 1
done
done
Now the modified program to include a 2 second left arrow is:
#!/bin/bash
#blinker
#creates a bi-stable process that displays ON and OFF
touch .running
clear
while [ -f .running ]
do
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 4 ]
then
break
fi
echo "GREEN"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 2 ]
then
break
fi
echo "YELLOW"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 4 ]
then
break
fi
echo "RED"
sleep 1
done
count=0
while [ -f .running ]
do
count=$((count+1))
if [ $count -gt 2 ]
then
break
fi
echo "<-"
sleep 1
done
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.