i need some help to build a script and i'm having some troubles.
so my code now is something like this:
Code:
#!/bin/bash
# global vars
interface=$1
mtu=$2
file="myfile.txt"
count=0
# function to validate mtu size
function validateMtu {
if [ $mtu -gt 0 ]; then
setMtu
else
echo "MTU should be bigger then 0"
fi
}
# function to set mtu size
function setMtu {
$count = { grep '^MTU=*' $file | wc -l }
if [ $count -gt 0 ]; then
mv $file $file"_back"
sed 's/^MTU=*/MTU=$mtu /' $file > output.txt
mv output.txt $file
else
echo "append to file"
fi
}
# script sequence
if [ $# -lt 2 ]; then
echo "Invalid number of parameters. Required 2 parameters"
echo "parameter 1: interface"
echo "parameter 2: value"
else
echo "setting mtu $mtu in interface $interface"
if [ -e $file ]; then
validateMtu
else
echo "File $file not found."
fi
fi
my 1st problem is that i don't know how to make grep results into variables, like the one in count.
assuming that is working, the sed command doesn't do exactly what i want
imagine i have in the file MTU=1500 and my new value is 1400 the result will be MTU=1400 1500
so it doesn't replace, the 1500 stays there.
the final problem is how to append to the file in case my string is not found.
i have tested the grep ^MTU=* $file | wc -l in the bash itself and worked, but in my script it doesn't and the script can't find the file also.
thanks in advance
edit:
this is the error now:
Code:
./testMtu.sh: line 19: 0: command not found
wc: }: No such file or directory
append to file :S
but i already tried 3 variations for line 19:
$count = { grep '^MTU=*' $file | wc -l }
$count = grep '^MTU=*' $file | wc -l
$count = grep ^MTU=* $file | wc -l
the last one works on the bash