摆脱“警告:命令替换:输入中被忽略的空字节"; [英] Get rid of "warning: command substitution: ignored null byte in input"

查看:1858
本文介绍了摆脱“警告:命令替换:输入中被忽略的空字节";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行model=$(cat /proc/device-tree/model)

bash --version
GNU bash, version 4.4.12(1)-release (arm-unknown-linux-gnueabihf)

使用bash 4.3.30版就可以了

With bash version 4.3.30 it's all OK

我知道问题是文件中的\0字符终止了,但是如何隐藏这个愚蠢的消息呢?自从我使用bash 4.4以来,我的整个脚本就搞砸了

I understand the problem is the terminating \0 character in the file, but how can I suppress this stupid message? My whole script is messed up since I'm on bash 4.4

推荐答案

在这里可能需要两种可能的行为:

There are two possible behaviors you might want here:

  • 读取直到第一个NUL.这是一种性能更高的方法,因为它不需要外壳的任何外部进程.在读取内容但输入中不存在NUL的情况下,检查失败后目标变量是否为非空可确保成功退出状态(否则将导致退出状态为非零).

  • Read until first NUL. This is the more performant approach, as it requires no external processes to the shell. Checking whether the destination variable is non-empty after a failure ensures a successful exit status in the case where content is read but no NUL exists in input (which would otherwise result in a nonzero exit status).

IFS= read -r -d '' model </proc/device-tree/model || [[ $model ]]

  • 读取时忽略所有NUL.这使您的行为等同于bash的较新版本(4.4).

  • Read ignoring all NULs. This gets you equivalent behavior to the newer (4.4) release of bash.

    model=$(tr -d '\0' </proc/device-tree/model)
    

    您还可以仅使用内置函数来实现它,如下所示:

    You could also implement it using only builtins as follows:

    model=""
    while IFS= read -r -d '' substring || [[ $substring ]]; do
      model+="$substring"
    done </proc/device-tree/model
    

  • 这篇关于摆脱“警告:命令替换:输入中被忽略的空字节";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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