Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Nothing Much.

TheGlock TheGlockMisc

:octocat:
Nothing Much.
  • Japan, Hokkaido
View GitHub Profile
@TheGlockMisc
TheGlockMisc / center_text_bash.sh
Last active Dec 31, 2021
Easiest way to Center Multiline Text in Bash
View center_text_bash.sh
#!/bin/bash
function Centered(){
echo "$1" | sed -e :a -e "s/^.\{1,$(tput cols)\}$/ & /;ta" | tr '\n' '\r'
}
# Using Pipe
function CenteredPipe(){
sed -e :a -e "s/^.\{1,$(tput cols)\}$/ & /;ta" /dev/stdin | tr '\n' '\r'
}
@TheGlockMisc
TheGlockMisc / unitconversion.sh
Last active Nov 18, 2021
Byte Unit Conversion with Decimal in Bash
View unitconversion.sh
#!/bin/bash
function UnitConversion(){
B=$1
KB=$(bc -l <<< "scale=2; $B/1024")
MB=$(bc -l <<< "scale=2; $B/1024/1024")
GB=$(bc -l <<< "scale=2; $B/1024/1024/1024")
TB=$(bc -l <<< "scale=2; $GB/1024")
[[ $(echo "$TB" | awk -F[.] '{print $1}') > 0 ]] && echo "$TB TB" && return
[[ $(echo "$GB" | awk -F[.] '{print $1}') > 0 ]] && echo "$GB GB" && return