title: Bash - function with optional arguments and missing logic
body: <p>I'm trying make simple function for the useradd command and to quickly improve my poor shell programming skills.</p>
  
  <pre><code>useradd -m -g [initial_group] -G [additional_groups] -s [login_shell] [username]
  </code></pre>
  
  <p>Right now I'm somewhat unsure how to function with optional arguments.  After some Googling and I think might have a handle on that, just need to play around the code.</p>
  
  <p>One thing that I'm unsure about is the logic and I'm curious on how you guys would go about writing this.  I'm sure it will be better then what I could hack together.</p>
  
  <hr>
  
  <p>Here is how I going to try setup my function arguments, for the login shell and the initial group I would them to have generic defaults.</p>
  
  <pre><code>arg1 - userName, required
  arg2 - loginShell, optional (default: /bin/bash)
  arg3 - initGroup, optional (default: users)
  arg4 - otherGroups, optional (default: none)
  </code></pre>
  
  <p>This is some lame pseudo code on how I'm thinking to structure this.</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <hr>
  
  <p>These are the links I'm going by</p>
  
  <p><a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html" rel="noreferrer">http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function">Passing parameters to a Bash function</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9332802/how-to-write-a-bash-script-that-takes-optional-input-arguments">How to write a bash script that takes optional input arguments?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13197207/using-functions-inside-here-document">Using Functions inside here document</a></p>