而IFS =读取-r -d $'\ 0'文件时...说明 [英] while IFS= read -r -d $'\0' file ... explanation

查看:160
本文介绍了而IFS =读取-r -d $'\ 0'文件时...说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这行shell脚本. while语句是否不需要会设置$的'test'或[]或[[]]表达式?到1或0?

I do not understand this line of shell script. Doesn't the while statement need a 'test' or [ ] or [[ ]] expression that will set $? to 1 or 0? How does

while IFS= read -r -d $'\0'; do ...; done

这样做吗?非常感谢您理解此处语法的任何帮助.

do that? Any help understanding the syntax here is greatly appreciated.

推荐答案

在Bash中,varname=value command在环境变量varname设置为value(以及所有其他环境变量)的情况下运行 command 正常继承).因此,IFS= read -r -d $'\0'在环境变量IFS设置为空字符串的情况下运行命令read -r -d $'\0',这意味着没有字段分隔符.

In Bash, varname=value command runs command with the environment variable varname set to value (and all other environment variables inherited normally). So IFS= read -r -d $'\0' runs the command read -r -d $'\0' with the environment variable IFS set to the empty string (meaning no field separators).

由于read每当成功读取输入且未遇到文件结尾时都返回成功(即,将$?设置为0),因此总体效果是遍历一组NUL分隔的集合记录(保存在变量REPLY中).

Since read returns success (i.e., sets $? to 0) whenever it successfully reads input and doesn't encounter end-of-file, the overall effect is to loop over a set of NUL-separated records (saved in the variable REPLY).

while语句是否不需要会设置$的'test'或[]或[[]]表达式?到1或0?

Doesn't the while statement need a 'test' or [ ] or [[ ]] expression that will set $? to 1 or 0?

test[ ... ][[ ... ]]实际上不是表达式,而是命令.在Bash中,每个命令返回成功(将$?设置为0)或失败(将$?设置为非零值,通常为1).

test and [ ... ] and [[ ... ]] aren't actually expressions, but commands. In Bash, every command returns either success (setting $? to 0) or failure (setting $? to a non-zero value, often 1).

(顺便说一句,如上面评论中的 nosid 所述,-d $'\0'等同于-d ''. Bash变量在内部表示为C样式/NUL终止的字符串,因此您实际上不能在字符串中包含NUL;例如,echo $'a\0b'仅打印a.)

(By the way, as nosid notes in a comment above, -d $'\0' is equivalent to -d ''. Bash variables are internally represented as C-style/NUL-terminated strings, so you can't really include a NUL in a string; e.g., echo $'a\0b' just prints a.)

这篇关于而IFS =读取-r -d $'\ 0'文件时...说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆