【发布时间】:2012-10-23 18:23:14
【问题描述】:
我正在尝试为 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?
【问题讨论】:
标签: bash