Solved

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

Posted on 2010-09-14
5
578 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
5 Comments
 
LVL 2

Assisted Solution

by:wilmaflintstone
wilmaflintstone earned 35 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
You're welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now