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
Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
remove spaces from a file name linux 3 65
Write an app 10 70
RoboCopy to Changing External Drives 2 52
cscript to activate Windows and Office? 2 20
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…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

861 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