我在做bash脚本,我提出了一个有趣的问题 要使用While,如果我使用while循环,它会如何影响系统的加载CPU 虽然循环执行/检查每个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
答案 0 :(得分:0)
你是对的,如果bash正在等待用户输入,while循环将不会消耗CPU。