Skip to content

Instantly share code, notes, and snippets.

@lloyd
Created February 10, 2014 16:10
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 lloyd/8918659 to your computer and use it in GitHub Desktop.
Save lloyd/8918659 to your computer and use it in GitHub Desktop.
#!/usr/bin/env dash
# arguments
section='epel-debuginfo'
key='gpgcheck'
value='0'
state="absent"
file="./epel.repo"
separator="="
# what is the line number of the desired section?
foundsection=""
# are we currently parsing through the desired section?
insection=0
# what is the line number of the found variable?
foundkey=""
contents=`cat -n ./epel.repo`
IFS='
'
for line in $contents; do
echo $line | egrep -q "\[.*\]"
if [ "$?" = "0" ]; then
# this is a section marker
echo $line | fgrep -q "[$section]"
if [ "$?" = "0" ]; then
foundsection=`echo $line | sed -e 's/^ *\([0-9]*\).*$/\1/'`
insection=1
else
insection=0
fi
fi
if [ "$insection" = "1" ]; then
echo $line | egrep -q "^\s*[0-9]*\s*$key"
if [ "$?" = "0" ]; then
foundkey=`echo $line | sed -e 's/^ *\([0-9]*\).*$/\1/'`
fi
fi
done
replaceline () {
file=$1
from=$2
to=$3
newline=$4
bakfile="$file".bak
mv $file $bakfile
head -$from $bakfile > $file
[ "x$newline" != "x" ] && echo $newline >> $file
tail -n+`expr $to + 1` $bakfile >> $file
}
if [ $state = "present" ]; then
newline=$key$separator$value
# if we want the variable to exist, and it does, we replace it
if [ "x$foundkey" != "x" ]; then
replaceline $file `expr $foundkey - 1` $foundkey $newline
else
replaceline $file $foundsection $foundsection $newline
fi
else
# if we want the variable not to exist, and it does, we delete it
if [ "x$foundkey" != "x" ]; then
replaceline $file `expr $foundkey - 1` $foundkey
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment