我正在尝试为useradd命令创建简单的函数,并快速提高我糟糕的shell编程技能。
useradd -m -g [initial_group] -G [additional_groups] -s [login_shell] [username]
现在我有点不确定如何使用可选参数进行操作。经过一些谷歌搜索,我认为可能有一个处理,只需要玩代码。
我不确定的一件事是逻辑,我很好奇你们将如何写这篇文章。我相信它会比我可以破解的更好。
以下是我将尝试设置我的函数参数的方法,对于登录shell和初始组,我希望它们具有通用默认值。
arg1 - userName, required
arg2 - loginShell, optional (default: /bin/bash)
arg3 - initGroup, optional (default: users)
arg4 - otherGroups, optional (default: none)
这是关于我如何构建这个问题的一些蹩脚的伪代码。
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;
}
}
这些是我要去的链接
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?
答案 0 :(得分:13)
您可能会发现${parameter:+word}
扩展很有用。来自Bash Reference Manual:
如果参数为null或未设置,则不替换任何内容,否则将替换 word 的扩展。
所以:
function addUser {
useradd -m ${2:+-s "$2"} ${3:+-g "$3"} ${4:+-G "$4"} "$1"
}
请注意,如果任何参数包含有趣的字符(如空格,美元符号或其他shell元字符),则此函数会正确处理引用。如果你试图拼凑一个命令字符串,那么正确引用这些部分要困难得多。如果这仅仅是为了您的个人短期使用并且您知道输入是安全的,那可能无关紧要。但是如果要以root身份运行并且不小心处理它的输入,最好不要留下脚本或函数。
答案 1 :(得分:7)
#!/bin/bash
启动脚本或使用bash
命令运行它)。如果您只使用基本的Bourne shell功能和语法,请使用sh(#!/bin/sh
或sh
命令)来运行它。如果你不知道,假设你需要bash。if [ -n "$2" ]; then
中,分号后面的空格是可选的(分号前面也可能有空格),但是其他空格的所有都是必需的(没有它们)命令会做一些完全不同的事情)。此外,在赋值中,不能是等号周围的空格,或者(再次)它会做一些完全不同的事情。 考虑到这一点,这是我对功能的看法:
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.
}
答案 2 :(得分:0)
google for ABS获取许多复杂的样本
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[@]}"
}
代码没有检查,但我希望我不会错过任何东西