我爲朋友編寫了這個腳本,以便在他的屏幕上顯示信息,但是無論何時運行它,他的CPU負載上升到接近80%,並且變得非常熱。我不知道這是他的計算機如何設置的問題,或者如果我寫這個bash效率非常低。導致高CPU使用率的腳本
#!/bin/sh
# Include config file.
. ~/.config/lemonbar/.config
# Fetch current network connection state -- and SSID, if available.
network(){
networkState=$(grep -R "up" /sys/class/net/*/operstate)
networkSSID=$(iwgetid -r)
if [[ ${networkState} != "" && ${networkSSID} != "" ]]; then
network=${networkSSID}
networkColor=${foreground}
elif [[ ${networkState} != "" ]]; then
network="Online"
networkColor=${foreground}
else
network="Offline"
networkColor=${red}
fi
echo "%{F$networkColor} ${network}"
}
# Fetch current dropbox sync status.
dropbox(){
dropboxInfo=$(dropbox-cli status)
if [[ $dropboxInfo == "Dropbox isn't running!" ]]; then
dropbox="Not Running"
dropboxColor=${red}
elif [[ $dropboxInfo == "Connecting..." || $dropboxInfo == "Starting..." ]]; then
dropbox="Connecting"
dropboxColor=${foreground}
elif [[ $(echo $dropboxInfo | grep "Syncing") != "" || $(echo $dropboxInfo | grep "Downloading") != "" || $(echo $dropboxInfo | grep "Indexing") != "" ]]; then
dropbox="Syncing"
dropboxColor=${green}
else
dropbox="Idle"
dropboxColor=${foreground}
fi
echo "%{F$dropboxColor} ${dropbox}"
}
# Fetch current volume state and mute state.
volume(){
volumeState=$(ponymix get-volume)
# Determine volume icon depending on the level of volume.
if [[ ${volumeState} -eq 0 ]]; then
volumeIcon=""
elif [[ ${volumeState} -le 50 ]]; then
volumeIcon=""
else
volumeIcon=""
fi
if $(ponymix is-muted); then
volumeColor=${red}
else
volumeColor=${foreground}
fi
echo "%{F$volumeColor}${volumeIcon} ${volumeState}%"
}
# Fetch the current percentage of the battery's capacity.
battery(){
if [[ -d /sys/class/power_supply/BAT0 ]]; then
batteryState=$(cat /sys/class/power_supply/BAT0/status)
batteryPower=$(cat /sys/class/power_supply/BAT0/capacity)
# Determine battery icon based on capacity and state.
if [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 20 ]]; then
batteryIcon=""
batteryColor=${red}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 40 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 60 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 80 ]]; then
batteryIcon=""
batteryColor=${foreground}
elif [[ "${batteryState}" == "Discharging" && ${batteryPower} -le 100 ]]; then
batteryIcon=""
batteryColor=${foreground}
else
batteryIcon=""
batteryColor=${green}
fi
echo "%{F$batteryColor}${batteryIcon} ${batteryPower}%"
else
echo "%{F$red}No Battery Detected"
fi
}
# Fetch the current date.
calendar(){
calendar=$(date "+%A, %b %d, %Y")
echo "%{F$foreground} ${calendar}"
}
# Fetch the current time.
clock(){
clock=$(date "+%I:%M %p")
echo "%{F$foreground} ${clock}"
}
# Format selected blocks for piping into bar.
status(){
currentSeparator="${separator}"
numberOfBlocks=$((${#selectedBlocks[@]} - 1))
for ((i=0; i<=${numberOfBlocks}; i++)); do
if [[ ${i} -eq ${numberOfBlocks} ]]; then
currentSeparator=""
fi
status+="$(${selectedBlocks[i]})${currentSeparator}"
done
echo "${status}"
}
# Pipe functions to the bar infinitely.
while true; do
echo "%{c}$(status)"
done | lemonbar -g ${panelWidth}x${panelHeight}+${panelX}+${topPanelY} -f "${font}-${fontSize}" -f "${iconFont}" -B "${background}" -p -d | \
while true; do read line; eval $line; done &
配置文件:
#!/bin/sh
# Import config files.
. ~/.universal
# Select which blocks are displayed on the status bar.
selectedBlocks=(
"network"
"dropbox"
"volume"
# "battery"
"calendar"
"clock"
)
# Bar panel settings
separator="%{F${accent}} :: "
gapSize=16
borderSize=4
panelHeight=$((${gapSize} * 2))
panelWidth=$((${screenWidth} - ${panelHeight}))
panelX=${gapSize}
topPanelY=${gapSize}
bottomPanelY=$((${screenHeight} - ${gapSize} - ${panelHeight}))
簡單的測試 - 用你的電腦上的CPU會發生什麼? –