LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-08-2008, 09:45 AM   #1
dreidu
LQ Newbie
 
Registered: Sep 2008
Distribution: Ubuntu
Posts: 6

Rep: Reputation: 0
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

Last edited by dreidu; 09-08-2008 at 09:52 AM.
 
Old 09-08-2008, 10:05 AM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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.
 
Old 09-08-2008, 10:26 AM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.

Last edited by weibullguy; 09-08-2008 at 10:28 AM.
 
Old 09-08-2008, 10:26 AM   #4
dreidu
LQ Newbie
 
Registered: Sep 2008
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
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

Last edited by dreidu; 09-08-2008 at 10:29 AM.
 
Old 09-08-2008, 10:39 AM   #5
dreidu
LQ Newbie
 
Registered: Sep 2008
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 09-08-2008, 10:41 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by dreidu View Post
still how can i force the overwrite of the file?
Try cp -f.
 
Old 09-08-2008, 10:46 AM   #7
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
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.
 
Old 09-08-2008, 10:46 AM   #8
dreidu
LQ Newbie
 
Registered: Sep 2008
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
wroking fine now :P

at least in red hat enterprise

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

feel free

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
 
Old 09-08-2008, 10:49 AM   #9
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Excellent
 
Old 09-08-2008, 11:07 AM   #10
dreidu
LQ Newbie
 
Registered: Sep 2008
Distribution: Ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by CRC123 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
BASH Scripting akilles Linux - Newbie 5 11-10-2004 07:26 PM
Bash Scripting Darklight451 Programming 1 10-07-2004 01:48 AM
About bash scripting pazvant Programming 3 10-20-2003 11:12 AM
Bash scripting NSKL Linux - General 2 06-08-2002 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration