Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.