9

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

Community
  • 1
  • 1
Dan
  • 1,179
  • 2
  • 10
  • 18

3 Answers3

16

You might find the ${parameter:+word} expansion useful. From the Bash Reference Manual:

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

So:

function addUser {
    useradd -m ${2:+-s "$2"} ${3:+-g "$3"} ${4:+-G "$4"} "$1"
}

Note that this function handles quoting properly if any of the arguments contain funny characters (like spaces, dollar signs, or other shell metacharacters). If you attempt to piece together a command string, it's much harder to quote the pieces properly. That might not matter if this is just for your personal, short-term use and you know the input is safe. But it's best not to leave a script or function lying around if it is intended to be run as root and doesn't handle its input very carefully.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks, validating the inputs was really a none issue since I'm doing random fooBar accounts, just too much Java on my end – Dan Nov 03 '12 at 06:16
12

@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.
}
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Doesn't this break if any optional argument is left out? For example, if the caller does not specify a loginShell but does specify initGroup and otherGroups, then the function will misinterpret the initGroup as the loginShell (because it's the 2nd arg) and the otherGroups as the initGroup (because it's the 3rd arg). – dg99 Jan 29 '18 at 23:17
  • 1
    @dg99 In that case you'd need to pass an empty string as the missing argument(s) to avoid confusion. For example: `addUser fred "" someGroup anotherGroup` would pass the empty string as `$2`, "someGroup" as `$3`, and "anotherGroup" as `$4`. – Gordon Davisson Jan 29 '18 at 23:26
0

google for ABS to get many complex samples

function addUser{
userName=$1;
loginShell=$2;
initGroup=$3
otherGroups=$4;

  args=(-m -g);

// Adding the initial user group
  if [[ $initGroup == '' ];then
    args+=(users);
  else 
    args+=("$initGrop");
  fi;

# Adding any additional groups
  if [[ $otherGroups != '' ]];then
    args+=(-G "$otherGroups");
  fi;

  if [[ $loginShell == '' ]];then
    args+=(-s /bin/bash "$userName");
  else
    args+=(-s "$loginShell" "$userName");
  fi;
  useradd "${args[@]}"
}

code not checked, but i hope i not miss anything

zb'
  • 8,071
  • 4
  • 41
  • 68
  • **code not checked, but i hope i not miss anything** it is sample, how to get options – zb' Apr 19 '14 at 01:35
  • 2
    Constructive comment: 1. Don't leave spaces within `=`: e.g., in the line `bashCmd = $bashCmd"users"`, Bash tries to execute the command `bashCmd` with the parameters `=` and whatever `${bashCmd}users` expands to. 2. [Don't put your commands in a variable!](http://mywiki.wooledge.org/BashFAQ/050) use an array instead. 3. `string` is very likely a `command not found` 4. typo `otherGropus` instead of `otherGroups`. 5. the ABS is not exactly a good reference! it has very bad reputation among Bash programmers (see, you're mentioning it and you give an awful answer). No offense `:)`. – gniourf_gniourf May 09 '14 at 13:28
  • the most problems was in original question :) but ok, I'll fix for your satisfaction, about ABC, i just not saw better learning guide for bash (like iptables-howto, o'c it is not the best and x-tables much more then them, but gives good intro) , so i think it is more about prefection, thank you for the comment btw. – zb' May 09 '14 at 21:48