core: 优化文本操作

This commit is contained in:
bin456789 2023-12-14 00:11:32 +08:00
parent 99c899beda
commit 2feffee1a2
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -441,14 +441,15 @@ to_lower() {
unix2dos() { unix2dos() {
target=$1 target=$1
# 先原地unix2dos出错再用复制,可最大限度保留文件权限 # 先原地unix2dos出错再用cat,可最大限度保留文件权限
if ! command unix2dos $target 2>/tmp/error.log; then if ! command unix2dos $target 2>/tmp/unix2dos.log; then
# 出错后删除 unix2dos 创建的临时文件 # 出错后删除 unix2dos 创建的临时文件
rm "$(awk -F: '{print $2}' /tmp/error.log | xargs)" rm "$(awk -F: '{print $2}' /tmp/unix2dos.log | xargs)"
tmp=$(mktemp) tmp=$(mktemp)
cp $target $tmp cp $target $tmp
command unix2dos $tmp command unix2dos $tmp
cp $tmp $target # cat 可以保留权限
cat $tmp >$target
rm $tmp rm $tmp
fi fi
} }
@ -458,11 +459,10 @@ insert_into_file() {
location=$2 location=$2
regex_to_find=$3 regex_to_find=$3
if [ "$location" = HEAD ]; then if [ "$location" = head ]; then
apk add ed bak=$(mktemp)
in=$(mktemp) cp $file $bak
cat /dev/stdin >$in cat - $bak >$file
echo -e "0r $in\n w \n q" | ed $file >/dev/null
else else
line_num=$(grep -E -n "$regex_to_find" "$file" | cut -d: -f1) line_num=$(grep -E -n "$regex_to_find" "$file" | cut -d: -f1)
@ -1134,19 +1134,20 @@ EOF
# 添加到 C:\Setup\Scripts\SetupComplete.cmd 最前面 # 添加到 C:\Setup\Scripts\SetupComplete.cmd 最前面
# call 防止子 bat 删除自身后中断主脚本 # call 防止子 bat 删除自身后中断主脚本
my_setup_complete=$(mktemp) setup_complete_mod=$(mktemp)
for bat in $bats; do for bat in $bats; do
echo "if exist %SystemDrive%\\$bat (call %SystemDrive%\\$bat)" >>$my_setup_complete echo "if exist %SystemDrive%\\$bat (call %SystemDrive%\\$bat)" >>$setup_complete_mod
done done
# 复制原来的内容
if [ -f $setup_complete ]; then if [ -f $setup_complete ]; then
# 直接插入而不是覆盖,可以保留权限,虽然没什么影响 cat $setup_complete >>$setup_complete_mod
insert_into_file $setup_complete HEAD <$my_setup_complete
else
cp $my_setup_complete $setup_complete
fi fi
unix2dos $setup_complete unix2dos $setup_complete_mod
# cat 可以保留权限
cat $setup_complete_mod >$setup_complete
fi fi
} }