ToB企服应用市场:ToB评测及商务社交产业平台

标题: Bash和Zsh中read下令的使用区别 [打印本页]

作者: 去皮卡多    时间: 2025-1-16 14:24
标题: Bash和Zsh中read下令的使用区别
前言:在编写sh脚本的时间,发现使用read下令和预期的效果不一样,,因为shell用的是zsh,查了一下发现bash和zsh中read下令的用法有些区别,这里记录一下。
读取字符

从标准输入中读取一个或多个字符
bash: read -n num input
zsh: read -k num input
例子:
  1. root@hcss-ecs-b5f1 ~ ❯ read -k 1 myinput                                           02:04:11 PM
  2. a
  3. root@hcss-ecs-b5f1 ~ ❯ echo $myinput                                               02:16:01 PM
  4. a
复制代码
提示符

在读取输入之前表现的提示符
bash: read -p "prompt" message
zsh: read "message?prompt",如果 read 下令的第一个参数包罗一个问号(?),那么问号后面的内容将被解释为提示符,而且当shell处于交互模式时,这个提示符会表如今标准错误输出(通常是终端)上。
例子:
  1. root@hcss-ecs-b5f1 ~ ❯ read "message?what?"                                        02:03:54 PM
  2. what?ssss
  3. root@hcss-ecs-b5f1 ~ 5s ❯ echo $message                                            02:04:06 PM
  4. ssss
复制代码
Zsh中的read下令及其参数说明

这部门是我从该文档[原文地址]中复制后机翻过来的。
通过man zshbuiltins,也可以查看zsh文档。
  1. read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
  2.      [ -u n ] [ [name][?prompt] ] [ name ... ]
复制代码
读取一行输入并根据$IFS变量中的字符作为分隔符将其分割成字段,下面有例外环境说明。第一个字段被赋值给第一个名称变量,第二个字段给第二个名称变量,以此类推,多余的字段则全部赋值给最后一个名称变量。如果省略了名称变量,则对于标量使用REPLY,而对于数组则使用reply。
以下为原文内容:
  1. read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
  2.      [ -u n ] [ [name][?prompt] ] [ name ... ]Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.-rRaw mode: a ‘\’ at the end of a line does not signify line continuation and backslashes in the line don’t quote the following character and are not removed.-sDon’t echo back characters if reading from the terminal.-qRead only one character from the terminal and set name to ‘y’ if this character was ‘y’ or ‘Y’ and to ‘n’ otherwise. With this flag set the return status is zero only if the character was ‘y’ or ‘Y’. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.-k [ num ]Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.Note that despite the mnemonic ‘key’ this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.-zRead one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with ‘print -z’ or with push-line from the line editor (see Zsh Line Editor). This flag is ignored when the -k or -q flags are present.-e-EThe input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.-AThe first name is taken as the name of an array and all words are assigned to it.-c-lThese flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the current command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.-nTogether with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.-u nInput is read from file descriptor n.-pInput is read from the coprocess.-d delimInput is terminated by the first character of delim instead of by newline.-t [ num ]Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns immediately if no input is available. If no input is available, return status 1 and do not set any variables.This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before reading, or within zle where other mechanisms should be used to test for input.Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually ‘read -t’ will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case, only availability of the first character is tested, so that e.g. ‘read -t -k 2’ can still block on the second character. Use two instances of ‘read -t -k’ if this is not what is wanted.If the first argument contains a ‘?’, the remainder of this word is used as a prompt on standard error when the shell is interactive.The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described for -q. Otherwise the value is 0.The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z cancels both -p and -u.The -c or -l flags cancel any and all of -kpquz.
复制代码
参考资料

read 在 zsh 和 bash 下的不同
The Z Shell Manual - Shell Builtin Commands

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4