Solved

Unix Shell script to prompt for an email address & insert it into 3rd line of a file

Posted on 2010-09-14
5
585 Views
Last Modified: 2013-12-26
A Shell script is preferred over a Perl script for this request.

From time to time I need to block a spam, so instead of going to the directory
& edit a file & save it, I would like to automate this a bit with a Shell script.
This script's requirements:

a) cd /folderA

b) make a backup of current file ie
       cp -p access_sender access_sender.ddmmyyyy  
    where ddmmyyyy is the day, month, year (in digits)

c) prompt for the offending/spam email address I wish to block & store into a variable ie
       Pls enter spam address to block: <this is where I'll key in the email address, eg: aa@bb.com>

d) use awk or nawk or sed to insert the email address entered into
     line 3 of access_sender plus a TAB followed by the text  REJECT.  Eg:
       aa@bb.com      REJECT
     Note: Lines 1 & 2 will stay while the former line 3, 4 onwards will get pushed down 1 line

e) then execute the command "postfix reload"

   
0
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Assisted Solution

by:wilmaflintstone
wilmaflintstone earned 35 total points
ID: 33674043
the way i would procede ( sorry for not giving you a script to run) is:

# ask for an email adress (using the read command)
# verify if what has been typed is ok
# copy the first 2 lines of the file to a new , temporary file (using the head -n 2 command)
# append the address followed by REJECT to this file (using the echo command)
# append the 3rd to the end of the file to the temp-file. (using the tail -n +3 command)
# rename the original file to a backupfile (mv original backup)
# rename the temp-file to be the original (mv temp-file original)
# restart the process (postfix reload)
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 65 total points
ID: 33674077
The attached will do it.  A small change (remove the prompt, and change "read addr" to "addr=$1") will allow you to specify the address on the command line.
cd /folder
mv access_sender access_sender.$new_date
echo Enter new SPAM address to block:
read addr
awk -vnew_line="$addr" '{print}NR == 2{print new_line "\tREJECT"}' access_sender.$new_date > access_sender
postfix reload

Open in new window

0
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 400 total points
ID: 33677883
Hi,

The below code will do exactly what you want.

Cheers,
K.

#!/bin/ksh
cd /folderA 
DT=$(date +%d%m%y)
cp -p access_sender  access_sender.$DT
read spam_addr?"Pls enter spam address to block: "
(awk 'NR==1;NR==2' access_sender.$DT;print - "$spam_addr\tREJECT" ;awk 'NR > 2' access_sender.$DT) > access_sender
postfix reload

Open in new window

0
 

Author Comment

by:sunhux
ID: 33683067

Thanks chaps.

KeremE's script meet my requirements exactly.

I'll be submitting another post to Shell Scripting zone for a similar request or in a way an additional
enhancement to KeremE's script, so look out for it
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 33684648
You're welcome.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question