Skip to content

Commit e70d174

Browse files
committed
[add] : Write functions in another file
1 parent dd073b0 commit e70d174

File tree

2 files changed

+104
-99
lines changed

2 files changed

+104
-99
lines changed

modules/share/airootfs.any/root/customize_airootfs.sh

Lines changed: 4 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
set -e -u
1111

1212

13-
# Default value
1413
# Default value
1514
# All values can be changed by arguments.
1615
password=alter
@@ -56,45 +55,15 @@ kernel_mkinitcpio_profile="${kernel_config_line[2]}"
5655
# Make it compatible with previous code
5756
unset OPTIND OPTARG arg
5857

58+
# Load functions
59+
source "/root/functions.sh"
5960

60-
# Check whether true or false is assigned to the variable.
61-
function check_bool() {
62-
local
63-
case $(eval echo '$'${1}) in
64-
true | false) : ;;
65-
*) echo "The value ${boot_splash} set is invalid" >&2 ;;
66-
esac
67-
}
6861

62+
# Check bool type
6963
check_bool boot_splash
7064
check_bool debug
7165

7266

73-
# Delete file only if file exists
74-
# remove <file1> <file2> ...
75-
function remove () {
76-
local _list
77-
local _file
78-
_list=($(echo "$@"))
79-
for _file in "${_list[@]}"; do
80-
if [[ -f ${_file} ]]; then
81-
rm -f "${_file}"
82-
elif [[ -d ${_file} ]]; then
83-
rm -rf "${_file}"
84-
fi
85-
echo "${_file} was deleted."
86-
done
87-
}
88-
89-
90-
function installedpkg () {
91-
if pacman -Qq "${1}" 1>/dev/null 2>/dev/null; then
92-
return 0
93-
else
94-
return 1
95-
fi
96-
}
97-
9867
# Enable and generate languages.
9968
sed -i 's/#\(en_US\.UTF-8\)/\1/' /etc/locale.gen
10069
if [[ ! "${localegen}" = "en_US\\.UTF-8\\" ]]; then
@@ -113,21 +82,6 @@ if [[ -f "/etc/skel/Desktop/calamares.desktop" ]]; then
11382
fi
11483

11584

116-
# user_check <name>
117-
function user_check () {
118-
if [[ ! -v 1 ]]; then return 2; fi
119-
getent passwd "${1}" > /dev/null
120-
}
121-
122-
# Execute only if the command exists
123-
# run_additional_command [command name] [command to actually execute]
124-
run_additional_command() {
125-
if [[ -f "$(type -p "${1}" 2> /dev/null)" ]]; then
126-
shift 1
127-
eval "${@}"
128-
fi
129-
}
130-
13185
usermod -s "${usershell}" root
13286
cp -aT /etc/skel/ /root/
13387
if [[ -f "$(type -p "xdg-user-dirs-update" 2> /dev/null)" ]]; then LC_ALL=C LANG=Cxdg-user-dirs-update; fi
@@ -136,40 +90,8 @@ echo -e "${password}\n${password}" | passwd root
13690
# Allow sudo group to run sudo
13791
sed -i 's/^#\s*\(%sudo\s\+ALL=(ALL)\s\+ALL\)/\1/' /etc/sudoers
13892

139-
# Create a user.
140-
# create_user <username> <password>
141-
function create_user () {
142-
local _password
143-
local _username
144-
145-
_username=${1}
146-
_password=${2}
147-
148-
set +u
149-
if [[ -z "${_username}" ]]; then
150-
echo "User name is not specified." >&2
151-
return 1
152-
fi
153-
if [[ -z "${_password}" ]]; then
154-
echo "No password has been specified." >&2
155-
return 1
156-
fi
157-
set -u
158-
159-
if ! user_check "${_username}"; then
160-
useradd -m -s ${usershell} ${_username}
161-
groupadd sudo
162-
usermod -U -g ${_username} ${_username}
163-
usermod -aG sudo ${_username}
164-
usermod -aG storage ${_username}
165-
cp -aT /etc/skel/ /home/${_username}/
166-
fi
167-
chmod 700 -R /home/${_username}
168-
chown ${_username}:${_username} -R /home/${_username}
169-
echo -e "${_password}\n${_password}" | passwd ${_username}
170-
set -u
171-
}
17293

94+
# Create user
17395
create_user "${username}" "${password}"
17496

17597

@@ -281,23 +203,6 @@ sed -i -r "s/(GRUB_DISTRIBUTOR=).*/\1\"${grub_os_name}\"/g" "/etc/default/grub"
281203
run_additional_command "gtk-update-icon-cache -f /usr/share/icons/hicolor"
282204

283205

284-
# systemctl helper
285-
# Execute the subcommand only when the specified unit is available.
286-
# Usage: _systemd_service <systemctl subcommand> <service1> <service2> ...
287-
_systemd_service(){
288-
local _service
289-
local _command="${1}"
290-
shift 1
291-
for _service in "${@}"; do
292-
# https://unix.stackexchange.com/questions/539147/systemctl-check-if-a-unit-service-or-target-exists
293-
if (( "$(systemctl list-unit-files "${_service}" | wc -l)" > 3 )); then
294-
systemctl ${_command} "${_service}"
295-
else
296-
echo "${_service} was not found" >&2
297-
fi
298-
done
299-
}
300-
301206
# Enable graphical.
302207
_systemd_service set-default graphical.target
303208

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bash
2+
3+
# Check whether true or false is assigned to the variable.
4+
function check_bool() {
5+
local
6+
case $(eval echo '$'${1}) in
7+
true | false) : ;;
8+
*) echo "The value ${boot_splash} set is invalid" >&2 ;;
9+
esac
10+
}
11+
12+
# Delete file only if file exists
13+
# remove <file1> <file2> ...
14+
function remove () {
15+
local _list
16+
local _file
17+
_list=($(echo "$@"))
18+
for _file in "${_list[@]}"; do
19+
if [[ -f ${_file} ]]; then
20+
rm -f "${_file}"
21+
elif [[ -d ${_file} ]]; then
22+
rm -rf "${_file}"
23+
fi
24+
echo "${_file} was deleted."
25+
done
26+
}
27+
28+
# user_check <name>
29+
function user_check () {
30+
if [[ ! -v 1 ]]; then return 2; fi
31+
getent passwd "${1}" > /dev/null
32+
}
33+
34+
# Execute only if the command exists
35+
# run_additional_command [command name] [command to actually execute]
36+
run_additional_command() {
37+
if [[ -f "$(type -p "${1}" 2> /dev/null)" ]]; then
38+
shift 1
39+
eval "${@}"
40+
fi
41+
}
42+
43+
44+
function installedpkg () {
45+
if pacman -Qq "${1}" 1>/dev/null 2>/dev/null; then
46+
return 0
47+
else
48+
return 1
49+
fi
50+
}
51+
52+
# Create a user.
53+
# create_user <username> <password>
54+
function create_user () {
55+
local _password
56+
local _username
57+
58+
_username=${1}
59+
_password=${2}
60+
61+
set +u
62+
if [[ -z "${_username}" ]]; then
63+
echo "User name is not specified." >&2
64+
return 1
65+
fi
66+
if [[ -z "${_password}" ]]; then
67+
echo "No password has been specified." >&2
68+
return 1
69+
fi
70+
set -u
71+
72+
if ! user_check "${_username}"; then
73+
useradd -m -s ${usershell} ${_username}
74+
groupadd sudo
75+
usermod -U -g ${_username} ${_username}
76+
usermod -aG sudo ${_username}
77+
usermod -aG storage ${_username}
78+
cp -aT /etc/skel/ /home/${_username}/
79+
fi
80+
chmod 700 -R /home/${_username}
81+
chown ${_username}:${_username} -R /home/${_username}
82+
echo -e "${_password}\n${_password}" | passwd ${_username}
83+
set -u
84+
}
85+
86+
# systemctl helper
87+
# Execute the subcommand only when the specified unit is available.
88+
# Usage: _systemd_service <systemctl subcommand> <service1> <service2> ...
89+
_systemd_service(){
90+
local _service _command="${1}"
91+
shift 1
92+
for _service in "${@}"; do
93+
# https://unix.stackexchange.com/questions/539147/systemctl-check-if-a-unit-service-or-target-exists
94+
if (( "$(systemctl list-unit-files "${_service}" | wc -l)" > 3 )); then
95+
systemctl ${_command} "${_service}"
96+
else
97+
echo "${_service} was not found" >&2
98+
fi
99+
done
100+
}

0 commit comments

Comments
 (0)