Skip to content

Instantly share code, notes, and snippets.

@kost
Created January 31, 2017 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kost/ac79a4a1bf0c9f1685c6f1ca41725cba to your computer and use it in GitHub Desktop.
Save kost/ac79a4a1bf0c9f1685c6f1ca41725cba to your computer and use it in GitHub Desktop.
#!/bin/sh
# nvram emulation shell script (c) kost, https://gist.github.com/kost
#
# Usage (on real device dump the content): nvram show > /tmp/nvram.file
# Copy script and /tmp/nvram.file to the emulation env, start using it
# In case you just need nvram command emulation, just copy script.
#
# uncomment for debug:
# set -x
show_usage() {
echo "usage: nvram [get name] [set name=value] [unset name] [commit] [show]"
return
}
if [ "$NVRAMFILE" = "" ]; then
NVRAMFILE="/tmp/nvram.file"
fi
if [ "$1" = "" ]; then
show_usage
fi
NVRAMCOMMIT="$NVRAMFILE.commit"
if [ -f "$NVRAMCOMMIT" ]; then
NVRAMACT="$NVRAMCOMMIT"
else
NVRAMACT="$NVRAMFILE"
fi
if [ "$1" = "show" ]; then
cat $NVRAMACT
fi
if [ ! -f "$NVRAMFILE" ]; then
touch "$NVRAMFILE" && (echo "Created $NVRAMFILE" >&2 )
fi
if [ "$1" = "commit" ]; then
if [ -f "$NVRAMFILE.commit" ]; then
cp $NVRAMFILE.commit $NVRAMFILE && rm -f $NVRAMFILE.commit
fi
fi
if [ "$1" = "get" ]; then
if [ "$2" = "" ]; then
show_usage
else
grep "^$2=" $NVRAMACT | head -1 | cut -f2- -d=
fi
fi
# we need tmp file for these commands
# mktemp would be better, but not portable (race cond)
NVRAMTMP="$NVRAMCOMMIT.$$"
if [ "$1" = "unset" ]; then
if [ "$2" = "" ]; then
show_usage
else
grep -v "^$2=" $NVRAMACT > $NVRAMTMP
cp $NVRAMTMP $NVRAMCOMMIT && rm $NVRAMTMP
fi
fi
if [ "$1" = "set" ]; then
if [ "$2" = "" ]; then
show_usage
else
NVRAMKEY=$(echo "$2" | cut -f1 -d=)
NVRAMVALUE=$(echo "$2" | cut -f2- -d=)
grep -v "^$NVRAMKEY=" $NVRAMACT > $NVRAMTMP
echo "$2" >> $NVRAMTMP
cp $NVRAMTMP $NVRAMCOMMIT && rm $NVRAMTMP
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment