I am trying to make a simple function for the useradd command and quickly improve my poor shell programming skills.
useradd -m -g [initial_group] -G [additional_groups] -s [login_shell] [username]
Right now I'm somewhat unsure of how to work with optional arguments. After some Googling, and I think it might be related to this, you just need to play around with the code.
One thing I'm not sure about is the logic, and I'm curious how you guys are going to write this. I am sure it will be better than I could hack together.
This is how I try to configure my function arguments, for the login shell and initial group I would have common default values.
arg1 - userName, required arg2 - loginShell, optional (default: /bin/bash) arg3 - initGroup, optional (default: users) arg4 - otherGroups, optional (default: none)
This is some kind of lame pseudo code about how I am going to structure this.
function addUser( userName, loginShell, initGroup, otherGroups){ // Not how I would go about this but you should get the point string bashCmd = "useradd -m -g "; // Adding the initial user group if(initGroup == null){ bashCmd += "users"; } else { bashCmd += initGrop; } // Adding any additional groups if(otherGropus != null){ bashCmd += " -G " + otherGroups; } if(loginShell == null){ bashCmd += " -s /bin/bash " + userName; } else { bashCmd += " -s " + loginShell + " " + userName; } }
These are the links I'm going to make
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
Passing Bash Function Parameters
How to write a Bash script that accepts optional input arguments?
Using functions inside a document