forked from Karmenzind/dotfiles-and-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonrc
More file actions
131 lines (111 loc) · 2.69 KB
/
Copy pathcommonrc
File metadata and controls
131 lines (111 loc) · 2.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env bash
bold='\033[1m'
origin='\033[0m'
black='\E[30;1m'
red='\E[31;1m'
green='\E[32;1m'
yellow='\E[33;1m'
blue='\E[34;1m'
magenta='\E[35;1m'
cyan='\E[36;1m'
white='\E[37;1m'
cut_off='--------------------------------------------'
# Color-echo
# $1: message
# $2: color
cecho() {
echo -e "${2:-${bold}}${1}"
tput sgr0 # Reset to normal.
}
put_error() {
cecho "$1" $red
}
exit_with_msg() {
cecho "$1" $red
exit -1
}
pacman_conf=/etc/pacman.conf
no_root() {
if [[ $USER = 'root' ]]; then
echo "Do not use root."
exit -1
fi
}
# $1 prompt
put_cutoff() {
_line="\n${cut_off}\n"
cecho $_line $cyan
if [[ -n "$1" ]]; then
echo -e "$1"
cecho $_line $cyan
fi
}
do_install() {
sudo pacman -S --needed --noconfirm "$@"
}
display_array() {
local tmp_array=($@)
local length=${#tmp_array[@]}
for (( i=0; i<$length; i++ )); do
echo "$i ${tmp_array[$i]}"
done
echo
}
# enhanced `read` - name a range for checking
# $1 input range, e.g. 123 Yn abcd (case insensitive)
# $2 variable's name
check_input() {
local _range=$1
local is_valid=no
local _default=${_range:0:1}
while [[ $is_valid = 'no' ]]; do
read -p "Input: " ans
[[ -z "$ans" ]] && ans=$_default
ans=`echo $ans | tr '[A-Z]' '[a-z]'`
if [[ "$_range" = *$ans* ]]; then
is_valid=yes
else
put_error "Valid answer: $_range (default=$_default):"
fi
done
[[ -n $2 ]] && read $2 <<< $ans
}
put_suspend() {
cecho "
Type Ctrl+C to exit
or type any key to continue" $cyan
read whatever
}
# -----------------------------------------------------------------------------
# basic
# -----------------------------------------------------------------------------
enable_multilib() {
sudo cat >>/etc/pacman.conf << EOF
[multilib]
Include = /etc/pacman.d/mirrorlist
EOF
}
check_multilib_support() {
echo 'Check multilib support ...'
if [[ -z "grep '^\[multilib\]' $pacman_conf" ]]; then
multilib_enabled=0
echo "Add multilib repo support? (Y/n)"
check_input yn
[[ $ans = 'y' ]] && enable_multilib && multilib_enabled=1
sudo pacman -Sy
else
multilib_enabled=1
fi
(( multilib_enabled == 0 )) && enable_multilib
echo "Support multilib: $multilib_enabled"
}
# -----------------------------------------------------------------------------
# basic
# -----------------------------------------------------------------------------
# $1 expr that will be passed to bc
# return 0 if nothing's wrong
valid_by_bc() {
local res
res=$(bc <<< "$1")
(( res == 1 )) || return 255
}