Files
backup_script/tools/core/base_utils.sh
2026-04-29 20:08:41 +08:00

173 lines
4.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/system/bin/sh
# module: base_utils.sh
echoRgb() {
#轉換echo顏色提高可讀性
if [[ $2 = 0 ]]; then
echo -e "\e[38;5;197m -!$1\e[0m"
elif [[ $2 = 1 ]]; then
echo -e "\e[38;5;121m -$1\e[0m"
elif [[ $2 = 2 ]]; then
echo -e "\e[38;5;${rgb_c}m -$1\e[0m"
elif [[ $2 = 3 ]]; then
echo -e "\e[38;5;${rgb_b}m -$1\e[0m"
else
echo -e "\e[38;5;${rgb_a}m -$1\e[0m"
fi
}
Set_back_0() {
return 0
}
Set_back_1() {
return 1
}
endtime() {
#計算總體切換時長耗費
case $1 in
1) starttime="${starttime1:-$(date -u "+%s")}" ;;
2) starttime="${starttime2:-$(date -u "+%s")}" ;;
*) starttime="$(date -u "+%s")" ;;
esac
endtime="$(date -u "+%s")"
duration="$(echo $((endtime - starttime)) | awk '{t=split("60 秒 60 分 24 時 999 天",a);for(n=1;n<t;n+=2){if($1==0)break;s=$1%a[n]a[n+1]s;$1=int($1/a[n])}print s}')"
[[ $duration != "" ]] && echo " -$2用時:$duration" || echo " -$2用時:0秒"
}
get_version() {
while :; do
keycheck
case $? in
42)
[[ $Select_user = true ]] && branch="$1" || branch=true
echoRgb "$1" "1"
;;
41)
[[ $Select_user = true ]] && branch="$2" || branch=false
echoRgb "$2" "0"
;;
*)
echoRgb "keycheck錯誤" "0"
continue
;;
esac
sleep 0.3
break
done
}
isBoolean() {
nsx="$1"
if [[ $1 = 1 ]]; then
nsx=true
elif [[ $1 = 0 ]]; then
nsx=false
else
echoRgb "$conf_path $2=$1填寫錯誤正確值1or0" "0"
Set_back_1
return 1
fi
}
echo_log() {
if [[ $? = 0 ]]; then
[[ $2 = "" ]] && echoRgb "$1成功" "1"
result=0
Set_back_0
else
echoRgb "$1失敗,過世了" "0"
notification "$RANDOM" "$name1: $1失敗,過世"
result=1
Set_back_1
fi
}
kill_Serve() {
LOCK_FILE="/tmp/backup_script_$$.lock"
if [[ -f $LOCK_FILE ]]; then
OLD_PID="$(cat "$LOCK_FILE")"
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "發現先前的備份程序 (PID=$OLD_PID),將其終止"
kill -KILL "$OLD_PID"
echo "結束自身,避免重複執行"
exit 1
else
echo "發現 lock 檔但程序已不存在,視為殘留 lock"
fi
fi
echo $$ > "$LOCK_FILE"
trap "rm -f '$LOCK_FILE'" EXIT
}
Show_boottime() {
awk -F '.' '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("%d天%d時%d分%d秒",run_days,run_hour,run_minute,run_second)}' /proc/uptime 2>/dev/null
}
Enter_options() {
echoRgb "$1" "2"
unset option parameter
while true ;do
if [[ $option != "" ]]; then
case $option in
0|1)
parameter="$option"
[[ $option = 1 ]] && echoRgb "$2" "2" || echoRgb "$3" "2"
break ;;
*)
echoRgb "$option參數錯誤 只能是0或1" "0"
read option ;;
esac
else
read option
fi
done
}
add_entry() {
app_name="$1"
package_name="$2"
# 檢查是否已經存在同樣的應用名稱
if [[ $(echo "$3" | cut -d' ' -f1 | grep -w "^$app_name$") = $app_name ]]; then
if [[ $(echo "$3" | cut -d' ' -f2 | grep -w "^$package_name$") != $package_name ]]; then
# 如果應用名稱存在但包名不同,則需要添加數字後綴
count=1
new_app_name="${app_name}_${count}"
while echo "$3" | grep -q "$new_app_name"; do
count=$((count + 1))
new_app_name="${app_name}_${count}"
done
app_name="$new_app_name"
fi
fi
REPLY="$app_name $package_name"
}
size() {
local b_size get_size
varr="$(echo "$1" | bc 2>/dev/null)"
if [[ $varr != $1 ]]; then
b_size="$(ls -l "$1" 2>/dev/null | awk '{print $5}')"
else
b_size="$1"
fi
if [[ $b_size -eq 0 ]]; then
get_size="0 bytes"
elif [[ $(echo "$b_size < 1024" | bc) -eq 1 ]]; then
get_size="${b_size} bytes"
elif [[ $(echo "$b_size < 1048576" | bc) -eq 1 ]]; then
get_size="$(echo "scale=2; $b_size / 1024" | bc) KB"
elif [[ $(echo "$b_size < 1073741824" | bc) -eq 1 ]]; then
get_size="$(echo "scale=2; $b_size / 1048576" | bc) MB"
else
get_size="$(echo "scale=2; $b_size / 1073741824" | bc) GB"
fi
echo "$get_size"
}
self_test() {
if [[ $(dumpsys deviceidle get charging) = false && $(dumpsys battery | awk '/level/{print $2}' | egrep -o '[0-9]+') -le 15 ]]; then
echoRgb "電量$(dumpsys battery | awk '/level/{print $2}' | egrep -o '[0-9]+')%太低且未充電\n -為防止備份檔案或是恢復因低電量強制關機導致檔案損毀\n -請連接充電器後備份" "0" && exit 2
fi
}
Validation_file() {
MODDIR_NAME="${1%/*}"
MODDIR_NAME="${MODDIR_NAME##*/}"
FILE_NAME="${1##*/}"
echoRgb "校驗$FILE_NAME"
case ${FILE_NAME##*.} in
zst) zstd -t "$1" 2>/dev/null ;;
tar) tar -tf "$1" &>/dev/null ;;
esac
echo_log "${FILE_NAME##*.}校驗"
}