linux工具

USB

cat_usb_device_power_wakeup.sh

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
#!/bin/bash
# author: Tom

fileName=""
productName=""

for file in /sys/bus/usb/devices/*/uevent; do
content=$(cat "$file")
if echo "$content" | grep -q "BUSNUM=$1" && echo "$content" | grep -q "DEVNUM=$2"; then
fileName="${file%/*}/power/wakeup"
productName="${file%/*}/product"

if [ -f "$fileName" ]; then
busnum=$(echo "$content" | grep -oE 'BUSNUM=([0-9]+)' | grep -oE '[0-9]+')
devnum=$(echo "$content" | grep -oE 'DEVNUM=([0-9]+)' | grep -oE '[0-9]+')

result=$(cat "$fileName")
productName=$(cat "$productName")
printf "Bus %3s Device %3s : %-30s power wakeup: %10s (路径: %s)\n" $busnum $devnum "$productName" $result "${file%/*}/product"
fi
fi
done

# 读取用户输入的设备节点和开关状态
read -p "请输入你要修改的设备的节点(例如 1-4.4.2 表示 BusNum-Port.Port.DevNum): " modify_device
read -p "请输入 enabled 或 disabled: " switch

# 检查用户输入的开关状态是否正确
if [[ "$switch" != "enabled" && "$switch" != "disabled" ]]; then
echo "无效输入。请输入 'enabled' 或 'disabled'。"
exit 1
fi

# 构造设备路径
device_path="/sys/bus/usb/devices/$modify_device/power/wakeup"

# 检查设备路径是否存在
if [ ! -f "$device_path" ]; then
echo "设备路径不存在或无效: $device_path"
exit 1
fi

# 修改设备的 power wakeup 状态
echo $switch | sudo tee "$device_path" > /dev/null

# 确认修改结果
new_result=$(cat "$device_path")
echo "设备 $modify_device 的 power wakeup 状态已设置为: $new_result"

GRUB

set_boot_kernel.sh

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
#!/bin/bash

# Usage: sudo ./set_boot_kernel.sh
# Author: tuhaowen

set -e

# GRUB 配置文件路径
GRUB_CFG="/boot/grub/grub.cfg"
GRUB_DEFAULT="/etc/default/grub"

# 检查 GRUB 配置文件是否存在
if [ ! -f "$GRUB_CFG" ]; then
echo "无法找到 GRUB 配置文件,请检查 GRUB 是否正确安装。"
exit 1
fi

# 获取内核列表
echo "正在提取可用内核版本..."
kernel_list=$(grep "menuentry" "$GRUB_CFG" | grep "Linux" | cut -d "'" -f2)

if [ -z "$kernel_list" ]; then
echo "未找到内核条目,请检查 GRUB 配置文件。"
exit 1
fi

# 显示内核列表
echo "检测到以下内核版本:"
echo "$kernel_list" | nl -w2 -s '. '

# 提示用户输入序号选择内核
read -p "请输入要设置为默认启动内核的序号: " choice

# 验证输入是否为有效数字
if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
echo "输入无效,请输入有效的数字序号。"
exit 1
fi

# 获取用户选择的内核条目
selected_kernel=$(echo "$kernel_list" | sed -n "${choice}p")

if [ -z "$selected_kernel" ]; then
echo "无效的序号,请重新运行脚本并选择有效序号。"
exit 1
fi

# 计算 GRUB_DEFAULT 的值
# 高级选项的序号固定为 "1",内核序号为 (choice - 1)
grub_entry="1>$((choice - 1))"

# 更新 GRUB 默认条目
echo "设置默认启动内核为: $selected_kernel"
if grep -q "^GRUB_DEFAULT=" "$GRUB_DEFAULT"; then
sed -i "s/^GRUB_DEFAULT=.*/GRUB_DEFAULT=\"$grub_entry\"/" "$GRUB_DEFAULT"
else
echo "GRUB_DEFAULT=\"$grub_entry\"" >> "$GRUB_DEFAULT"
fi

# 更新 GRUB 配置
echo "更新 GRUB 配置..."
sudo update-grub

# 确保文件系统同步
echo "同步文件系统..."
sync

echo "默认启动内核已更新为: $selected_kernel (GRUB_DEFAULT=\"$grub_entry\")"
echo "请重新启动系统以应用更改。"

linux工具
https://tomwithkernel.github.io/script/README/
作者
Tom
发布于
2024年5月20日
更新于
2025年3月26日
许可协议