Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ver="nvramfile v0.6" # rs232 - 09/23
- b="\033[0;40m"
- w="\033[1;37m"
- alias helpout='echo -e "┌───────────────────────────────────────────────────────────────────────────────┐
- │ ${w}FreshTomato $ver${b} │
- └───────────────────────────────────────────────────────────────────────────────┘
- ┌─ Standard operation ──────────────────────────────────────────────────────────┐
- │ ${w}nvramfile get|set|unset variable /path/file*${b}
- │ ${w}nvramfile show /path/file*${b}
- │
- │ * path/file is optional. If left empty this default to \$(nvram get nvfile)
- │
- │ usage examples:
- │ nvramfile get 'adblock_blacklist' /mnu/USB/config.nvram
- │ or
- │ nvram set nvfile=/mnu/USB/config.nvram
- │ nvramfile get 'adblock_blacklist'
- └───────────────────────────────────────────────────────────────────────────────┘
- ┌─ Migration ───────────────────────────────────────────────────────────────────┐
- │ ${w}nvramfile copy|move <var regex> source destination ${b}
- │ ${w}nvramfile remove <var regex> /path/file*${b}
- │
- │ • source & destination - can be \"n\" (nvram) or \"f\" (file)
- │ • f only - gets the config file full path from \$(nvram get nvfile)
- │ • f only - providing manually the full file path is also accepted
- │ • remove - allows to unset variable in the file (only)
- │ • variables ending \"_enable\" and \"_vmfile\" are excluded (always stay in nvram)
- │ • <var regex> - to copy a single variable do append \"=\" to the nariable name
- │ * path/file if optional, is left empty this is taken from \$(nvram get nvfile)
- │
- │ usage example:
- │ nvramfile move ^adblock_ n /mnt/USB/config.nvram
- │ or
- │ nvram set nvfile=/mnu/USB/config.nvram
- │ nvramfile move ^adblock_ n f
- │
- │ nvramfile remove ^adblock_
- └───────────────────────────────────────────────────────────────────────────────┘
- " && exit'
- [ "$#" -eq 0 ] && helpout || {
- [ $(nvram get nvfile | wc -c ) -gt 0 ] && nvfile="$(nvram get nvfile)"
- nvfileset() {
- echo "${2}" | grep -q '=' && {
- vn=$(echo "${2}" | grep -Eo '.*=')
- grep -q ^$vn "${3}" && {
- sed -i "/^${vn}/c ${2}" "${3}"
- } || echo "${2}" >> "${3}"
- } || echo "Error the variable is not fully defined"
- }
- nvfileunset() {
- vn=$(echo ${2} | cut -d= -f1) ; vn="${vn}="
- grep -q ^$vn "${3}" && {
- sed -i "/^${vn}/d" "${3}"
- } || echo -e "Variable not found in ${3}"
- }
- if [ "${1}" == 'help' ]; then
- helpout
- elif [ "${1}" == 'get' ]; then
- [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
- [ ! -z "${3}" ] && nvfile="${3}"
- cat "${nvfile}" | grep -E "${2}"!\=$ | cut -d= -f2
- exit
- elif [ "${1}" == 'set' ]; then
- [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
- [ ! -z "${3}" ] && nvfile="${3}"
- nvfileset "${1}" "${2}" "$nvfile"
- exit
- elif [ "${1}" == 'unset' ]; then
- [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
- [ ! -z "${3}" ] && nvfile="${3}"
- nvfileunset "${1}" "${2}" "$nvfile"
- exit
- elif [ "${1}" == 'show' ]; then
- [ ! -z "${2}" ] && nvfile="${2}"
- cat "${nvfile}"
- elif [ "${1}" == 'copy' ]; then
- [ -z "${2}" -o -z "${3}" -o -z "${4}" ] && { echo -e \"parameter missing\"; exit; }
- # Copy nvram to file
- [ "${3}" == 'n' -a ! -z "${4}" ] && {
- [ "${4}" != 'f' ] && nvfile="${4}"
- nvram show | grep -v '_enable=\|_vmfile=' | grep -E "${2}" | while read line; do
- nvfileset "${1}" "${line}" "$nvfile"
- done
- }
- # Copy file to nvram
- [ ! -z "${3}" -a "${4}" == 'n' ] && {
- [ "${3}" != 'f' ] && nvfile="${3}"
- cat "$nvfile" | grep -E "${2}"= | while read line; do
- nvram set "${line}"
- done
- nvram commit
- }
- exit
- elif [ "${1}" == 'move' ]; then
- # Move nvram to file
- [ "${3}" == 'n' -a ! -z "${4}" ] && {
- [ "${4}" != 'f' ] && nvfile="${4}"
- nvram show | grep -v '_enable=\|_vmfile=' | grep -E "${2}" | while read line; do
- vn=$(echo ${line} | cut -d= -f1)
- nvfileset "${1}" "${line}" "${nvfile}"
- nvram unset "${vn}"
- done
- nvram commit
- }
- # Move file to nvram
- [ ! -z "${3}" -a "${4}" == 'n' ] && {
- [ "${3}" != 'f' ] && nvfile="${3}"
- cat "${nvfile}" | grep -E "${2}" | while read line; do
- vn=$(echo ${line} | cut -d= -f1)
- nvram set "${line}"
- nvfileunset "${1}" "$vn" "$nvfile"
- done
- nvram commit
- }
- elif [ "${1}" == 'remove' ]; then
- [ -z "${2}" ] && { echo -e \"parameter missing\"; exit; }
- [ ! -z "${3}" ] && nvfile="${3}"
- grep -E "${2}" "$nvfile" | cut -d= -f1 | while read line ; do nvfileunset "${1}" "${line}" "${nvfile}"; done
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement