我正在编写 bash 脚本,我想出了一个有趣的问题 To use While 和 if I use while loop 这如何影响系统的负载 CPU While 循环在每个 CPU 节拍/周期中执行/检查 - 所以 cpu 不是空闲的 - 对吗?我用 wwwhat() 和 ww() 做了一点乒乓效应——这意味着 CPU 在等待用户输入时会处于空闲状态?我想知道有时这是否比使用循环(C、C++、Java 等)更好的性能解决方案?
#!bin/bash
wwwhat(){
read x
if [ "$x" == "y" ]; then echo "Starting WEB SERVER"; service httpd start 2> /dev/null;
echo "Loading web site in 10sec with w3m"
sleep 10
w3m 127.0.0.1
exit
fi
if [ "$x" == "n" ]; then echo "End";
else ww; # ask Y or N
fi
}
sstartw(){
echo;
}
ww(){ #back to wwwhat
echo "y/n?:"
wwwhat;
}
clear
printf "TODAY: "; date;
echo
s="WHOAMI: ";printf "$s " ;whoami;
echo
h="HOSTNAME: "; printf "$h " ; hostname
echo
a="1";
if service httpd status > /dev/null ; then printf "WEB SERVER IS RUNNING";
echo
echo "Loading web site in 10sec with w3m"
sleep 10
w3m 127.0.0.1
fi
if ! service httpd status > /dev/null ; then printf "WEB SERVER IS DOWN";
echo
echo "Do you want to start the WEB SERVER?(y/n)"; wwwhat;
fi