Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - function with optional arguments and missing logic

Tags:

bash

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

like image 570
Dan Avatar asked Nov 03 '12 05:11

Dan


People also ask

How do you make an argument in a function optional?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.

How do you indicate optional arguments?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.


2 Answers

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.

like image 91
rob mayoff Avatar answered Sep 26 '22 23:09

rob mayoff


@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.
}
like image 24
Gordon Davisson Avatar answered Sep 26 '22 23:09

Gordon Davisson