Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:27:11+00:00 2026-06-13T18:27:11+00:00

I’m trying make simple function for the useradd command and to quickly improve my

  • 0

I’m trying make simple function for the useradd command and to 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 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.

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.


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.

arg1 - userName, required
arg2 - loginShell, optional (default: /bin/bash)
arg3 - initGroup, optional (default: users)
arg4 - otherGroups, optional (default: none)

This is some lame pseudo code on how I’m thinking 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 by

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

Passing parameters to a Bash function

How to write a bash script that takes optional input arguments?

Using Functions inside here document

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:27:12+00:00Added an answer on June 13, 2026 at 6:27 pm

    @rob mayoff’s answer is the simplest way to accomplish this, but I thought I’d take a stab at turning your pseudo code into real shell syntax to point out some standard gotchas for people used to “real” programming languages. Three general notes first:

    • Different shells have different capabilities, so use bash (i.e. start your script with #!/bin/bash or run it with the bash command) if you need any bash extensions. If you are only using basic Bourne shell features and syntax, run it with sh (#!/bin/sh or the sh command) instead. If you don’t know, assume you need bash.
    • When building a command for later execution, there are all sorts of parsing oddities you can run into (see BashFAQ#050: I’m trying to put a command in a variable, but the complex cases always fail!). The best way is generally to build it as an array, rather than a string. ‘Course, arrays are a bash extension, not a basic shell feature…
    • In shell syntax, spaces matter. For instance, in the command if [ -n "$2" ]; then, the space after the semicolon is optional (and there could also be a space before the semicolon), but all of the other spaces are required (without them the command will do something completely different). Also, in an assignment, there cannot be spaces around the equal sign, or (again) it’ll do something completely different.

    With that in mind, here’s my take on the function:

    addUser() {
    # The function keyword is optional and nonstandard, just leave it off. Also,
    # shell functions don't declare their arguments, they just parse them later
    # as $1, $2, etc
    
    bashCmd=(useradd -m)
    # you don't have to declare variable types, just assign to them -- the
    # parentheses make this an array. Also, you don't need semicolons at the
    # end of a line (only use them if you're putting another command on the
    # same line). Also, you don't need quotes around literal strings, because
    # everything is a string by default. The only reason you need quotes is to
    # prevent/limit unwanted parsing of various shell metacharacters and such.
    
    # Adding the initial user group
    if [ -z "$3" ]; then
    # [ is actually a command (a synonym for test), so it has some ... parsing
    # oddities. The -z operator checks whether a string is empty (zero-length).
    # The double-quotes around the string to be tested are required in this case,
    # since otherwise if it's zero-length it'll simply vanish. Actually, you
    # should almost always have variables in double-quotes to prevent accidental
    # extra parsing.
    # BTW, since this is a bash script, we could use [[ ]] instead, which has
    # somewhat cleaner syntax, but I'm demonstrating the difficult case here.
        bashCmd+=(-g users)
    else
        bashCmd+=(-g "$3")
        # Here, double-quotes here are not required, but a good idea in case
        # the third argument happens to contain any shell metacharacters --
        # double-quotes prevent them from being interpreted here. -g doesn't
        # have any shell metacharacters, so putting quotes around it is not
        # necessary (but wouldn't be harmful either).
    fi
    
    # Adding any additional groups
    if [ -n "$4" ]; then
        bashCmd+=(-G "$4")
    fi
    
    # Set the login shell
    if [ -z "$2" ]; then
        bashCmd+=(-s /bin/bash "$1")
    else
        bashCmd+=(-s "$2" "$1")
    fi
    
    # Finally, run the command
    "${bashCmd[@]}"
    # This is the standard idiom for expanding an array, treating each element
    # as a shell word.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.