add_perm.sh 400 B

1234567891011121314151617181920212223242526272829
  1. #!/bin/bash
  2. if [ $# -lt 2 ]; then
  3. echo "Usage: $0 <add_perm> <file>"
  4. echo " ex. $0 0666 /dev/ttyUSB0"
  5. exit 1
  6. fi
  7. P=$1
  8. F=$2
  9. if [ ! -e $F ]; then
  10. echo "Not found $F"
  11. exit 1
  12. fi
  13. S="0$(stat -c %a $F)" # ex. "0666"
  14. if [ $(($S & $P)) -ne $(($P)) ]; then
  15. M=$(echo "obase=8; $(($S | $P))" | bc)
  16. echo "Please input password for chmod $F"
  17. sudo chmod "0$M" $F || exit 1
  18. fi
  19. exit 0
  20. # EOF