summaryrefslogtreecommitdiff
path: root/src/lib/xilib.sh
blob: 61db15671d040975865d16d49d6684893b4fcf1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh

# format a number into a bytes, kibibytes, mebibytes, or gibibytes
#
format_bytes () {
    case "1" in 
        "$(($1>=1<<30))") printf "$(($1>>30))GiB";;
        "$(($1>=1<<20))") printf "$(($1>>20))MiB";;
        "$(($1>=1<<10))") printf "$(($1>>10))kiB";;
        *) printf "$1B";;
    esac
}

# ensure that the user is a root user
#
checkroot () {
    [ "$(id -u)" = "0" ] || {
        printf "${RED}Please run as root!${RESET}\n"
        exit 1
    }
}

# reverse the order of lines
#
reverse_lines () {
    local result=
    while IFS= read -r line; do 
        result="$line
        $result"
    done
    echo "$result" 
}


#!/bin/sh

basename () {
    name=$1
    suf=$2
    pre=${name%/*}
    name=${name#$pre/}

    echo ${name#$suf}
}