functions.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # newline
  2. N="
  3. "
  4. append() {
  5. local var="$1"
  6. local value="$2"
  7. local sep="${3:- }"
  8. eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  9. }
  10. load_modules() {
  11. (sed "s,^[^#][^[:space:]]*,insmod /lib/modules/$(uname -r)/&.ko," $* | sh 2>&- || :)
  12. }
  13. user_exists() {
  14. grep -q "^$1:" $IPKG_INSTROOT/etc/passwd 2>&-
  15. }
  16. group_exists() {
  17. grep -q "^$1:" $IPKG_INSTROOT/etc/group 2>&-
  18. }
  19. service_exists() {
  20. grep -q "^$1[[:space:]]*$2" $IPKG_INSTROOT/etc/services 2>&-
  21. }
  22. rcconf_exists() {
  23. grep -q "^#*$1=" $IPKG_INSTROOT/etc/rc.conf 2>&-
  24. }
  25. add_user() {
  26. user_exists $1 || {
  27. echo "adding user $1 to /etc/passwd"
  28. echo "$1:x:$2:${3:-$2}:$1:${4:-/tmp}:${5:-/bin/false}" \
  29. >>$IPKG_INSTROOT/etc/passwd
  30. }
  31. }
  32. add_group() {
  33. group_exists $1 || {
  34. echo "adding group $1 to /etc/group"
  35. echo "$1:x:$2:$3" >>$IPKG_INSTROOT/etc/group
  36. }
  37. }
  38. add_service() {
  39. service_exists $1 $2 || {
  40. echo "adding service $1 to /etc/services"
  41. printf '%s\t%s\n' "$1" "$2" >>$IPKG_INSTROOT/etc/services
  42. }
  43. }
  44. add_rcconf() {
  45. rcconf_exists ${2-$1} || {
  46. echo "adding service ${2-$1} to /etc/rc.conf"
  47. printf '%s="%s"\t\t# %s\n' "${2:-$1}" "${3:-NO}" "$1" \
  48. >>$IPKG_INSTROOT/etc/rc.conf
  49. }
  50. }
  51. get_next_uid() {
  52. uid=1
  53. while grep "^[^:]*:[^:]*:$uid:" $IPKG_INSTROOT/etc/passwd \
  54. >/dev/null 2>&1; do
  55. uid=$(($uid+1))
  56. done
  57. echo $uid
  58. }
  59. get_next_gid() {
  60. gid=1
  61. while grep "^[^:]*:[^:]*:$gid:" $IPKG_INSTROOT/etc/group \
  62. >/dev/null 2>&1; do
  63. gid=$(($gid+1))
  64. done
  65. echo $gid
  66. }