ianw1974 Posted March 28, 2006 Report Share Posted March 28, 2006 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!) Quote Link to comment Share on other sites More sharing options...
Guest Hobbletoe Posted March 28, 2006 Report Share Posted March 28, 2006 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. Quote Link to comment Share on other sites More sharing options...
jboy Posted March 28, 2006 Report Share Posted March 28, 2006 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 ... Quote Link to comment Share on other sites More sharing options...
neddie Posted March 28, 2006 Report Share Posted March 28, 2006 What were the errors when you tried the suggestion in your previous thread? Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 28, 2006 Author Report Share Posted March 28, 2006 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. Quote Link to comment Share on other sites More sharing options...
ianw1974 Posted March 29, 2006 Author Report Share Posted March 29, 2006 Neddie, Not sure why, but it worked this time :unsure: I must have screwed it up the first time. Thanks for all your help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.