Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-14
5
Medium Priority
?
587 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 140 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 20

Assisted Solution

by:simon3270
simon3270 earned 260 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 1600 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.
Suggested Courses

721 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