Created
June 4, 2020 03:12
-
-
Save cfraizer/33e0e2ae1845cd17a15b6b3e00fe7e54 to your computer and use it in GitHub Desktop.
Pure Bash vs. Sed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo() { | |
local -a statusValues=( | |
"Discharging" | |
"Not Charging" | |
"Charging" | |
"Unknown" | |
"Full" | |
) | |
local -i valCount=0 | |
valCount="${#statusValues[@]}" | |
local -i loopCount=1000 | |
local -i i=0 | |
for (( i = 0; i < loopCount; ++i )); do | |
local status="${statusValues[$RANDOM % valCount]}" | |
local warn=""; | |
(( RANDOM % 2 == 0 )) && warn="YOW!" | |
local capacity="$(( RANDOM % 101 ))" | |
status="${status//,/}" | |
status="${status/#Discharging/Bat-}" | |
status="${status/#Not Charging/RedSq}" | |
status="${status/#Charging/Bat+}" | |
status="${status/#Unknown/Bat}" | |
status="${status/#Full/Bat+}" | |
status="${status} " | |
capacity="${capacity}%" | |
printf "%s%s%s\n" "$status" "$warn" "$capacity" | |
done | |
} | |
bar() { | |
local -a statusValues=( | |
"Discharging" | |
"Not Charging" | |
"Charging" | |
"Unknown" | |
"Full" | |
) | |
local -i valCount=0 | |
valCount="${#statusValues[@]}" | |
local -i loopCount=1000 | |
local -i i=0 | |
for (( i = 0; i < loopCount; ++i )); do | |
local status="${statusValues[$RANDOM % valCount]}" | |
local warn=""; | |
(( RANDOM % 2 == 0 )) && warn="YOW!" | |
local capacity="$(( RANDOM % 101 ))" | |
printf "%s%s%s\n" "$(echo "$status" | sed -e \ | |
"s/,//\ | |
;s/Discharging/Bat- / \ | |
;s/Not Charging/RedSq / \ | |
;s/Charging/Bat+ / \ | |
;s/Unknown/Bat / \ | |
;s/Full/Bat+ /")" "$warn" "$(echo "$capacity" | sed -e 's/$/%/' )" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment