LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-help-668385/)

dreidu 09-08-2008 09:45 AM

Bash scripting help
 
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

CRC123 09-08-2008 10:05 AM

Problem 1:

In Bash, there are two ways of storing output into a variable:
Code:

variable=`command` # using backtic's
or
variable=$(command) #using $(), I don't know what they're called, lol

Also note that there is no need to put a '$' in front of variable when storing data to it. Only to need to place a '$' when dereferencing them.

Problem 2 (sed command):
You need to create a capture group and use regular expressions to only capture numbers:

Code:

sed -r 's/^MTU=([0-9]*)/MTU=$mtu \1 /' $file > output.txt
-r turns on regular expressions
()'s form a capture group of everything that is inside them
'[0-9]' is regular expression for matching only numbers
* match more than 1 digit
\1 will place the first capture group at that spot.

Problem 3 (append to file):
to append to the end of a file use '>>'
Code:

echo "append to file" >> file.txt
Note: instead of this "grep '^MTU=*' $file | wc -l " I would use
Code:

grep -c '^MTU=*...
the -c will give you the count of how many times MTU shows up in the file.

weibullguy 09-08-2008 10:26 AM

You don't use the $ when creating a variable, only when dereferencing it.
Code:

count=$(grep '^MTU=*' $file | wc -l)
You could do the sed inline instead of redirecting it to a new file that is immediately renamed as the old file.
Code:

sed -i "s/^MTU=[0-9]*/MTU=$mtu/" $file
One line at a time
Code:

echo MTU=$mtu >> $file
EDIT: Yeah, what CRC123 said. The () is a sub-shell. Placing the $ in front makes the results of the command inside a variable.

dreidu 09-08-2008 10:26 AM

thanks it seems to be almost working now.

only have two more questions, one is different now

who do i say when i do this

Code:

cp $file $file"_back"
force it to do so, i mean it's not working because the file needs to be replaced,

because i get a warning

cp: overwrite `myfile.txt_back'?


the other problem is still in the beginning

`grep -c '^MTU=*' $file`

i still have:

Code:

[root@localhost test_scripts]# ./setmtu.sh eth0 1444
setting mtu 1444 in interface eth0
./setmtu.sh: line 19: 0: command not found

edit:

Code:

[root@localhost test_scripts]# sed -r 's/^MTU=([0-9]*)/MTU=1350 \1 /' myfile.txt > output.txt
[root@localhost test_scripts]# ls
1st.sh        helloworld.sh  myfile.txt2  myfile.txt5      read.sh
arguments.sh  myfile.txt    myfile.txt3  myfile.txt_back  setmtu.sh
exit.sh      myfile.txt1    myfile.txt4  output.txt      testMtu.sh
[root@localhost test_scripts]# cat output.txt
aaa
aaa
aaMTU=1500
MTU=1350 1500
sasdsa
sdadsads
asdsad

still some problems in the sed the 1500 refuses to disappear :P

but if i use:
sed -r 's/^MTU=([0-9]*)/MTU=1350 \1 /' myfile.txt > output.txt
it works

so thanks

dreidu 09-08-2008 10:39 AM

almost done :P

Quote:

#!/bin/bash
# global vars
interface=$1
mtu=$2
file="myfile.txt"

# 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 {
if [ `grep -c "^MTU=*" $file` -gt 0 ]; then
cp $file $file"_back"
sed -r 's/^MTU=([0-9]*)/MTU=$mtu /' $file > output.txt
mv output.txt $file
else
echo "MTU="$mtu >> $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
the only thing not working now is that the replace i guess because
sed -r 's/^MTU=([0-9]*)/MTU=$mtu /' $file > output.txt
i have to make some changes to instead of MTU=$mtu it changes to MTU=VALUE

maybe '... MTU='$mtu ' /' i'll try

still how can i force the overwrite of the file?

colucix 09-08-2008 10:41 AM

Quote:

Originally Posted by dreidu (Post 3273421)
still how can i force the overwrite of the file?

Try cp -f.

CRC123 09-08-2008 10:46 AM

Oops, single quotes in sed command won't expand variables, use same syntax but use double quotes:

sed -r "s/^MTU=([0-9]*)/MTU=$mtu \1 /" $file > output.txt

Just for clarification
assume $mtu is 1400 and the line "MTU=1500" appears in a file named file.txt

Do you want the sed command to make that line read as "MTU=1400 1500" or "MTU=1400"?


As far as your grep error goes, your calling $count by itself somewhere(on line 19). Its running contents of $count (which is the digit 0) and reporting that there is no command named '0'. Check your code syntax, or post it here.

dreidu 09-08-2008 10:46 AM

wroking fine now :P

at least in red hat enterprise :D

if you need a basic script for changing you mtu size in a given interface

feel free :D

Quote:

#!/bin/bash
# global vars
interface=$1
mtu=$2
file="/etc/sysconfig/network-scripts/ifcfg-"$interface

# 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 {
if [ `grep -c "^MTU=*" $file` -gt 0 ]; then
cp $file $file"_back"
sed -r 's/^MTU=([0-9]*)/MTU='$mtu' /' $file > output.txt
mv output.txt $file
else
echo "MTU="$mtu >> $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
thanks guys for the help

CRC123 09-08-2008 10:49 AM

Excellent :)

dreidu 09-08-2008 11:07 AM

Quote:

Originally Posted by CRC123 (Post 3273424)
Just for clarification
assume $mtu is 1400 and the line "MTU=1500" appears in a file named file.txt

Do you want the sed command to make that line read as "MTU=1400 1500" or "MTU=1400"?

it's always to stay just the value in the input

it's just for replace

thanks it's working now :D


All times are GMT -5. The time now is 05:44 AM.