"read -p"是什么意思?在Linux Shell脚本中做什么?
我有一个已复制和编辑的脚本.如果可能的话,我需要解释几行.
I have a script that I have copied and edited. There are a couple of lines in there that I need explaining if possible please.
以下是这些行:
read -p "please enter the username you wish to create: " username
if id -u $username >/dev/null 2>&1; then
read -p
的作用是什么? id -u
的作用是什么? >/dev/null 2&1;
的作用是什么?
然后在脚本中继续,它的这一行内容如下:
What does read -p
do? What does id -u
do? What does >/dev/null 2&1;
do?
Then further on in the script, it has this line that says this:
sudo useradd -g $group -s $bash -d $homedir -m $username -p $password
再次请有人解释这行中的所有减号吗? (-g
,-s
,-d
,-m
,-p
)
Again please could someone explain all the minus signs in this line please? (-g
, -s
, -d
, -m
, -p
)
首先,结构<command> -<option>
表示您要使用与<option>
相对应的选项执行<command>
.命令后的-
表示以下字母是一个选项.大多数命令都有几个可以使用的选项.选项通常使用单个字母或几个用-
分隔的单词来定义.
First off, the structure <command> -<option>
means that you want to execute <command>
using the option corresponding to <option>
. A -
after a command means that the following letter is an option. Most commands have several options you can use. Options are usually defined using either a single letter or a couple of words separated by -
.
旁注:对于包含几个单词而不是单个字母的选项,通常会使用两个减号--
而不是一个,表示它是长命名"选项.
Side Note: For options that are a couple of words rather than a single letter, often it will use two minus signs --
instead of one, signifying that it is a "long named" option.
因此,使用read -p
示例,这意味着您想使用p
选项执行read
,该选项代表提示.
So, using the read -p
example, this means you want to execute read
using the p
option, which stands for prompt.
现在,有时选项将需要一个参数.在您的示例中,useradd
的选项具有参数.参数的定义通常类似于<command> -<option> [argument]
.因此,在useradd
示例中,$group
是选项g
的参数.
Now, sometimes an option will require an argument. In your examples, the options to useradd
have arguments. Arguments are usually defined like <command> -<option> [argument]
. So, in the useradd
example, $group
is an argument for the option g
.
现在输入命令本身:
read
是一个内置的bash(不是POSIX shell命令),它读取来自标准输入.
read
is a bash built-in (not a POSIX shell command) that reads from standard input.
-
-p
选项使它作为提示阅读,这意味着它在尝试读取输入之前不会添加尾随换行符.
- The
-p
option makes it read as a prompt, meaning it doesn't add a trailing newline before trying to read input.
if
检查测试的返回状态命令(在本例中为id -u $username >/dev/null 2>&1
)
- 如果返回状态为0,则执行
then
部分
id
打印用户组和ID
id
prints user groups and ids
-
-u
选项仅打印有效的用户ID". -
>/dev/null 2>&1
将标准输入和标准错误重定向到/dev/null
,这意味着它们不会打印到终端上.
- The
-u
option "prints only the effective user ID". -
>/dev/null 2>&1
redirects standard input and standard error to/dev/null
, meaning they do not get printed to the terminal.
useradd
创建一个新用户
useradd
creates a new user
-
-g
为用户设置初始组 -
-s
设置用户登录shell的名称 -
-d
设置用户登录目录的名称 -
-m
表示要创建用户的主目录(如果该目录不存在). -
-p
定义用户的加密密码.
-
-g
sets the initial group for the user -
-s
sets the name of the user's login shell -
-d
sets the name of the user's login directory -
-m
says to create the user's home directory if it does not exist. -
-p
defines the user's encrypted password.
供以后参考,您可以通过在命令行上执行man <command>
在linux手册页中查找命令.这些手册页告诉您命令的功能,并说明命令的所有选项.
For future reference, you can look up commands in the linux manual pages by doing man <command>
on the command line. These manual pages tell you what a command does, as well as explaining all of its options.
像read
这样的Bash内置控件都位于单个手册页上,这不是最容易使用的东西.对于那些我来说,搜索它们比较容易.通常,结果中会出现 http://ss64.com/,其中包含来自bash内置函数的信息手册页,但通过命令分为不同的页面.我发现这更容易使用.
Bash built-ins like read
are all on a single man page that is not the easiest thing to use. For those I find googling them easier. Usually http://ss64.com/ will come up in the results, which contains the info from the bash built-ins man page, but separated into different pages by command. I find this much easier to use.