Register
It is currently Fri Apr 25, 2014 3:21 am

not working from bash script, but working directly in termin


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Oct 14, 2011 11:32 am   

Joined: Fri Oct 14, 2011 11:22 am
Posts: 1
I am trying to make a bash script, really simple one, that comments out any line in file.txt that contains user input value, in my case email address.

The script looks like this:


#!/bin/bash
echo Unblock email account:
read email
sed -i '/$email/s/^/#/' file.txt
______________________________
- file.txt before running script:

user@example.com REJECT
__________________________________
- file.txt should look like this after running script:

#user@example.com REJECT

But it is not working, the file stays unchanged.
And if I execute sed -i '/user@example.com/s/^/#/' file.txt in terminal, It does the job.

Any idea on how to get this work from script?


Tnx!


Top
 Profile  
 PostPosted: Fri Oct 14, 2011 1:49 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi denmaddog!

If I had to guess, I suspect that sed isn't translating your $email variable, since it's in 'single quotes'.

Try enclosing your sed command in "double quotes" instead. OR you could break the variable out of the quotes, like this:

Code:
sed -i '/'$email'/s/^/#/' file.txt


I hope this helps!


Top
 Profile YIM  
 PostPosted: Sat Nov 05, 2011 10:21 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You're editing a file. Use ed(1).

Code:
#!/bin/bash

read -rp 'Unblock email account: '

ed -s file.txt < <(printf '%s\n' "g/${REPLY}/ s/^/#/" w)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: DarthWavy and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP