I am new to awk/bash scripting and trying to write a bash script to simplify the process of adding an email account to my postfix email server on a linux box. Im stuck at awk. Anyone with Postfix experience can see what i am doing. To add a mailbox the admin must add the user, then add a line to the "virtual" file. To insert the line, the script needs to: find the line which has the word "virtual" and the mailsuffix stored in the "x[$n]" variable which the user selected.I cant seem to figure out how to insert everything in the $Line variable into the virtual file. It cant just be inserted into the end of the file. It needs to go at the end os a certain section. For example, if the user chooses 1 at the prompt (@ibm.com). the text in the login variable needs to go into the virtual file at the first blank line after "ibm.com virtual".
Here is what I have so far:
********************************
#!/bin/bash
LOGIN="$1"
NUMARG=$#
if [ $NUMARG = 0 ]; then
echo "Please enter a username"
exit 1
fi
if id "$LOGIN" >/dev/null 2>&1 ; then
echo "User $LOGIN already exists"
exit 1
fi
n=1
for name in $(awk '$2 == "virtual" { print $1 }' virtual )
do
echo "$n) $name"
x[$n]=$name
let "n += 1"
done
echo "Please chose a mail suffix for $LOGIN (1-$n)"
read choice
line="$LOGIN@${x[$choice]} $LOGIN@mydomain.org"
echo "$line"
postmap /etc/postfix/virtual
postfix reload
adduser $LOGIN
echo "User $LOGIN added"
echo ""
passwd $LOGIN
********************************
Contents of virtual:
********************************
ibm.com virtual
info@ibm.com info@mydomain.org
sales@ibm.com sales@mydomain.org
dell.com virtual
butthead@dell.com butthead@mydomain.org
hr@dell.com hr@mydomain.org
********************************
thanks in advance
Here is what I have so far:
********************************
#!/bin/bash
LOGIN="$1"
NUMARG=$#
if [ $NUMARG = 0 ]; then
echo "Please enter a username"
exit 1
fi
if id "$LOGIN" >/dev/null 2>&1 ; then
echo "User $LOGIN already exists"
exit 1
fi
n=1
for name in $(awk '$2 == "virtual" { print $1 }' virtual )
do
echo "$n) $name"
x[$n]=$name
let "n += 1"
done
echo "Please chose a mail suffix for $LOGIN (1-$n)"
read choice
line="$LOGIN@${x[$choice]} $LOGIN@mydomain.org"
echo "$line"
postmap /etc/postfix/virtual
postfix reload
adduser $LOGIN
echo "User $LOGIN added"
echo ""
passwd $LOGIN
********************************
Contents of virtual:
********************************
ibm.com virtual
info@ibm.com info@mydomain.org
sales@ibm.com sales@mydomain.org
dell.com virtual
butthead@dell.com butthead@mydomain.org
hr@dell.com hr@mydomain.org
********************************
thanks in advance