Advertisement
rs232

nvramfile

Aug 30th, 2023 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.33 KB | None | 0 0
  1. #!/bin/sh
  2. ver="nvramfile v0.6" # rs232 - 09/23
  3. b="\033[0;40m"
  4. w="\033[1;37m"
  5. alias helpout='echo -e "┌───────────────────────────────────────────────────────────────────────────────┐
  6. │                          ${w}FreshTomato $ver${b}                           │
  7. └───────────────────────────────────────────────────────────────────────────────┘
  8. ┌─ Standard operation ──────────────────────────────────────────────────────────┐
  9. │     ${w}nvramfile get|set|unset variable /path/file*${b}
  10. │     ${w}nvramfile show /path/file*${b}
  11. │ * path/file is optional. If left empty this default to \$(nvram get nvfile)
  12. │ usage examples:
  13. │     nvramfile get 'adblock_blacklist' /mnu/USB/config.nvram
  14. │ or
  15. │     nvram set nvfile=/mnu/USB/config.nvram
  16. │     nvramfile get 'adblock_blacklist'
  17. └───────────────────────────────────────────────────────────────────────────────┘
  18. ┌─ Migration ───────────────────────────────────────────────────────────────────┐
  19. │     ${w}nvramfile copy|move <var regex> source destination ${b}
  20. │     ${w}nvramfile remove <var regex> /path/file*${b}
  21. │ • source & destination - can be \"n\" (nvram) or \"f\" (file)
  22. │ • f only - gets the config file full path from \$(nvram get nvfile)
  23. │ • f only - providing manually the full file path is also accepted
  24. │ • remove - allows to unset variable in the file (only)
  25. │ • variables ending \"_enable\" and \"_vmfile\" are excluded (always stay in nvram)
  26. │ • <var regex> - to copy a single variable do append \"=\" to the nariable name
  27. │ * path/file if optional, is left empty this is taken from \$(nvram get nvfile)
  28. │ usage example:
  29. │     nvramfile move ^adblock_ n /mnt/USB/config.nvram
  30. │ or
  31. │     nvram set nvfile=/mnu/USB/config.nvram
  32. │     nvramfile move ^adblock_ n f
  33. │    
  34. │     nvramfile remove ^adblock_
  35. └───────────────────────────────────────────────────────────────────────────────┘
  36. " && exit'
  37.  
  38. [ "$#" -eq 0 ] && helpout || {
  39. [ $(nvram get nvfile | wc -c ) -gt 0 ] && nvfile="$(nvram get nvfile)"
  40. nvfileset() {
  41.     echo "${2}" | grep -q '=' && {
  42.         vn=$(echo "${2}" | grep -Eo '.*=')
  43.         grep -q ^$vn "${3}" && {
  44.         sed -i "/^${vn}/c ${2}" "${3}"
  45.         } || echo "${2}" >> "${3}"
  46.     } || echo "Error the variable is not fully defined"
  47. }
  48. nvfileunset() {
  49.     vn=$(echo ${2} | cut -d= -f1) ; vn="${vn}="
  50.     grep -q ^$vn "${3}" && {
  51.         sed -i "/^${vn}/d" "${3}"
  52.     } || echo -e "Variable not found in ${3}"
  53. }
  54.  
  55. if [ "${1}" == 'help' ]; then
  56.     helpout
  57. elif [ "${1}" == 'get' ]; then
  58.     [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
  59.     [ ! -z "${3}" ] && nvfile="${3}"
  60.     cat "${nvfile}" | grep -E "${2}"!\=$ | cut -d= -f2
  61.     exit
  62. elif [ "${1}" == 'set' ]; then
  63.     [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
  64.     [ ! -z "${3}" ] && nvfile="${3}"
  65.     nvfileset "${1}" "${2}" "$nvfile"
  66.     exit
  67. elif [ "${1}" == 'unset' ]; then
  68.     [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
  69.     [ ! -z "${3}" ] && nvfile="${3}"
  70.     nvfileunset "${1}" "${2}" "$nvfile"
  71.     exit
  72. elif [ "${1}" == 'show' ]; then
  73.     [ ! -z "${2}" ] && nvfile="${2}"
  74.     cat "${nvfile}"
  75. elif [ "${1}" == 'copy' ]; then
  76.     [ -z "${2}" -o -z "${3}" -o -z "${4}" ] && { echo -e \"parameter missing\"; exit; }
  77. # Copy nvram to file
  78.     [ "${3}" == 'n' -a ! -z "${4}" ] && {
  79.         [ "${4}" != 'f' ] && nvfile="${4}"
  80.         nvram show | grep -v '_enable=\|_vmfile=' | grep -E "${2}" | while read line; do
  81.         nvfileset "${1}" "${line}" "$nvfile"
  82.     done
  83.     }
  84. # Copy file to nvram
  85.     [ ! -z "${3}" -a "${4}" == 'n' ] && {
  86.         [ "${3}" != 'f' ] && nvfile="${3}"
  87.         cat "$nvfile" | grep -E "${2}"= | while read line; do
  88.         nvram set "${line}"
  89.         done
  90.         nvram commit
  91.     }
  92.     exit
  93. elif [ "${1}" == 'move' ]; then
  94. # Move nvram to file
  95.     [ "${3}" == 'n' -a ! -z "${4}" ] && {
  96.         [ "${4}" != 'f' ] && nvfile="${4}"
  97.         nvram show | grep -v '_enable=\|_vmfile=' | grep -E "${2}" | while read line; do
  98.         vn=$(echo ${line} | cut -d= -f1)
  99.         nvfileset "${1}" "${line}" "${nvfile}"
  100.         nvram unset "${vn}"
  101.         done
  102.         nvram commit
  103.     }
  104. # Move file to nvram
  105.     [ ! -z "${3}" -a "${4}" == 'n' ] && {
  106.         [ "${3}" != 'f' ] && nvfile="${3}"
  107.         cat "${nvfile}" | grep -E "${2}" | while read line; do
  108.         vn=$(echo ${line} | cut -d= -f1)
  109.         nvram set "${line}"
  110.         nvfileunset "${1}" "$vn" "$nvfile"
  111.         done
  112.         nvram commit
  113.     }
  114. elif [ "${1}" == 'remove' ]; then
  115.     [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
  116.     [ ! -z "${3}" ] && nvfile="${3}"
  117.     grep -E "${2}" "$nvfile" | cut -d= -f1 | while read line ; do nvfileunset "${1}" "${line}" "${nvfile}"; done
  118. fi
  119. }
Tags: nvramfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement