Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Using sed with bash variables


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Question Using sed with bash variables

Hi Guys

I have another problem I'm trying to solve and hope that some one can help me here.

This is the scenario:

I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE.

Below is the bash script I'm using.
Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -ie '3i\$WEBSITE\' /etc/squid3/acls/allowed-sites
        echo "restarting squid"
fi

I have tried a few different ways of doing this with sed but nothing works.

Please help.



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data

Last edited by rbatte1; 06-04-2015 at 06:27 AM.. Reason: CODE tags and spelling/grammar corrections
# 2  
Please use code tags as required by forum rules!

The single quotes prevent the variable from expansion, so the literal name is inserted. Try using double quotes instead, and don't escape the $ with the \ . Some seds insist on a line break before the insertion text.
# 3  
I changed the code to this

Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -i -e "3i$WEBSITE\" /etc/squid3/acls/allowed-sites
fi

and now im getting this error

Code:
/usr/local/sbin/add-website.sh: line 19: unexpected EOF while looking for matching `"'
/usr/local/sbin/add-website.sh: line 22: syntax error: unexpected end of file

Not sure where to go from here
# 4  
Quote:
Originally Posted by linuxjunkie
I changed the code to this

Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -i -e "3i$WEBSITE\" /etc/squid3/acls/allowed-sites
fi

and now im getting this error

Code:
/usr/local/sbin/add-website.sh: line 19: unexpected EOF while looking for matching `"'
/usr/local/sbin/add-website.sh: line 22: syntax error: unexpected end of file

Not sure where to go from here
my crystal ball says there's no matching double-quote on line 19 of your script.
sed -i -e "3i$WEBSITE\" /etc/squid3/acls/allowed-sites
What exactly is the purpose of \"?
# 5  
Question

if u use the command like this

Code:
 sed -i -e "3i/$WEBSITE/g" /etc/squid3/acls/allowed-sites

then i get this in the file
Code:
/mm.mm/g

Im new to using sed so i dont really know what all the quotes and things mean yet.
# 7  
That is correct

---------- Post updated at 04:43 PM ---------- Previous update was at 04:36 PM ----------

I managed to fix it. The old code was

Code:
sed -ie '3i$WEBSITE\' /etc/squid3/acls/allowed-sites

The correct code to allow the variable to work is

Code:
sed -i -e '3i'$WEBSITE'\' /etc/squid3/acls/allowed-sites

So now if i run the script and use debian.com as the input text for the variable then it adds the text debian.com to the file allowed-sites.

Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -i -e '3i'$WEBSITE'\' /etc/squid3/acls/allowed-sites
        echo "restarting squid"
        /etc/init.d/squid3 restart
fi

Thanks for the help guys !.

Last edited by linuxjunkie; 06-04-2015 at 11:55 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Passing awk variables to bash variables
urello
Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work... Shell Programming and Scripting
2
Shell Programming and Scripting
BASH arrays and variables of variables in C++
frad
Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:...... Shell Programming and Scripting
10
Shell Programming and Scripting
Complex bash/sed, variables and nested quotes
nocloud
Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >...... Shell Programming and Scripting
3
Shell Programming and Scripting
Bash variables
RichieFondel
Ummm can anybody help me with this one? Its prob quite simple. I bascially have a file name say J1x2x3x7.dat Im using the file name as a variable in a bash script. Want I want to do is extract most of the file name and make it a new variable expect with say one of the number now a...... Shell Programming and Scripting
2
Shell Programming and Scripting
using sed on bash variables (or maybe awk?)
rev66
Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ...... Shell Programming and Scripting
2
Shell Programming and Scripting

Featured Tech Videos