Skip to content

Instantly share code, notes, and snippets.

@UncleBrook
Created June 6, 2024 03:39
Show Gist options
  • Save UncleBrook/870b2fd16ca03cdfae19d86d3857c982 to your computer and use it in GitHub Desktop.
Save UncleBrook/870b2fd16ca03cdfae19d86d3857c982 to your computer and use it in GitHub Desktop.
Android: Fix adb devices no permissions
add_udev_rule() {
local rules_file=/etc/udev/rules.d/51-android.rules
local idVendor
local idProduct
# 根据参数数量分别处理
case "$#" in
1) # 一个参数,应该是 idVendor:idProduct
if [[ "$1" =~ ^([0-9a-fA-F]{4}):([0-9a-fA-F]{4})$ ]]; then
idVendor=$(echo "$1" | cut -d ':' -f 1)
idProduct=$(echo "$1" | cut -d ':' -f 2)
else
echo "Invalid input. Usage for single argument: add_udev_rule <idVendor:idProduct>"
echo "Example: add_udev_rule 05c6:90db"
return 1
fi
;;
2) # 两个参数,分别是 idVendor 和 idProduct
idVendor="$1"
idProduct="$2"
;;
*) # 其他情况,参数数量不正确
echo "Usage: add_udev_rule <idVendor> <idProduct>"
echo "Or: add_udev_rule <idVendor:idProduct>"
return 1
;;
esac
local rule="SUBSYSTEM==\"usb\", ATTR{idVendor}==\"$idVendor\", ATTR{idProduct}==\"$idProduct\", MODE=\"0666\", GROUP=\"plugdev\""
if grep -Fxq "$rule" "$rules_file"; then
echo "The rule already exists in the file.\n"
sudo cat "$rules_file"
return 1
fi
echo "$rule" | sudo tee -a "$rules_file" > /dev/null
sudo udevadm control --reload-rules
sudo cat "$rules_file"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment