Jump to content

Script help if values aren't completed [solved]


Recommended Posts

I created the following script for creating new mail users:

 

#!/bin/bash
# Script for adding email users to the system.
#
# Parameters have to be passed at the command line.
#
# Command line:	newmailuser username password domainname
#
# These parameters are then passed using variables
# $1, $2 and $3 as shown in the script.
#
echo
echo Creating user:.......................... $1
echo with password:.......................... $2
echo for email use with domain name:......... $3
echo
echo Email address is:....................... $1@$3

# Create Mail Directory and assign vmail rights.
mkdir /home/vmail/$1
maildirmake /home/vmail/$1/.maildir
chown -R vmail:vmail /home/vmail/$1
echo
echo User directory is: /home/vmail/$1
echo Mail directory is: /home/vmail/$1/.maildir

# Update mysql database with user information.
echo "insert into users values('','$1@$3','$2','$1',1001,1001,'/home/vmail/$1/','/home/vmail/$1/.maildir/','','y');" > ~/mailuser.sql
mysql -u mailsql -pmailsql mailsql < ~/mailuser.sql
rm -f ~/mailuser.sql
echo
echo New mail user created in mysql database.
echo

 

however, I want to make the script fail and/or display pointers on how the script should be used if they just type "newmailuser" (which is the script name).

 

How could I make it check if the variables haven't been completed, and not run the script, as currently it will run through the script and fail because of the lack of information.

 

I know my mysql insert record thing probably isn't the cleanest, but this was the only way I could get this part to work :P (help appreciated on this if someone knows a better way!)

Link to comment
Share on other sites

Guest Hobbletoe

Put it all in a case statement ...

 

case "$#" in 
 "3")
# Enter your script here;;
 "*")
# Enter help info here;;
esac

 

It is an easy way check to see if you have the correct number of arguments.

Link to comment
Share on other sites

You can check that the script was called with the 3 required parameters and exit if not. This checks that exactly 3 are supplied, no more no less.

 

if [ "$#" -ne 3 ]
then
 echo "You must specify all of the following: username password domainname"
 exit 1
fi

...

 

 

This tests that at least 3 are supplied.

 

if [ "$#" -lt 3 ]
then
 echo "You must specify all of the following: username password domainname"
 exit 1
fi

...

Link to comment
Share on other sites

I can't remember what the errors were, it just didn't seem to work. I'll post tomorrow, I shall try the script again with the previous commands, plus with the new ones too.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...