POSIX shell 中的换行符

Jur*_*sic 3 unix shell scripting

我正在解析 avahi-browse 工具的输出,我的脚本应该与 POSIX 兼容。

我正在这样做:

      local _dnssd=`avahi-browse -apt`
      if [ -z "$_dnssd" ]; then
        echo "No info"
      else
        IFS='
    ' # it's new line character in IFS
        for _row in $_dnssd
         do  
          local _tmpIFP="$IFS"
          IFS=";"
            case "$_row" in
            ...
            esac
            IFS="$_tmpIFS"
        done
      fi  
Run Code Online (Sandbox Code Playgroud)

我真的不喜欢将换行符分配给 IFS。是否有可能以更好的方式替换它?

我尝试了 stackoverflow 的一些建议,但它不起作用:

IFS=$(echo -e '\n')
Run Code Online (Sandbox Code Playgroud)

avahi浏览输出:

+;br0;IPv4;switch4B66E4;_http._tcp;local
+;br0;IPv4;switch4B66E4;_csco-sb._tcp;local
Run Code Online (Sandbox Code Playgroud)

小智 6

\nIFS变量后面添加一个空格,然后再次删除该空格:

IFS="$(printf '\n ')" && IFS="${IFS% }"
#IFS="$(printf '\n ')" && IFS="${IFS%?}"
printf '%s' "$IFS" | od -A n -c 
Run Code Online (Sandbox Code Playgroud)