Solved

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

Posted on 2010-09-14
5
581 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to eliminate duplicates from a list of hosts in groups 2 57
check unix curl command return value 7 115
mobaxterm not able to change directory 28 148
aix unix tar error 3 66
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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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 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…

770 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