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 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 $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 |
Problem 1:
In Bash, there are two ways of storing output into a variable: Code:
variable=`command` # using backtic's 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 ()'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 Code:
grep -c '^MTU=*... |
You don't use the $ when creating a variable, only when dereferencing it.
Code:
count=$(grep '^MTU=*' $file | wc -l) Code:
sed -i "s/^MTU=[0-9]*/MTU=$mtu/" $file Code:
echo MTU=$mtu >> $file |
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" 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 Code:
[root@localhost test_scripts]# sed -r 's/^MTU=([0-9]*)/MTU=1350 \1 /' myfile.txt > output.txt but if i use: sed -r 's/^MTU=([0-9]*)/MTU=1350 \1 /' myfile.txt > output.txt it works so thanks |
almost done :P
Quote:
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? |
Quote:
|
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. |
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:
|
Excellent :)
|
Quote:
it's just for replace thanks it's working now :D |
All times are GMT -5. The time now is 05:44 AM. |