Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newline character in POSIX shell

I'm parsing output of avahi-browse tool and my script should be POSIX compatible.

I'm doing it next way:

      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  

I really don't like line with newline assignment to IFS. Is it possible to replace it in better way?

I tried some suggestions from stackoverflow, but it doesn't work:

IFS=$(echo -e '\n')

avahi-browse output:

+;br0;IPv4;switch4B66E4;_http._tcp;local
+;br0;IPv4;switch4B66E4;_csco-sb._tcp;local
like image 831
Jurasic Avatar asked Dec 06 '25 02:12

Jurasic


1 Answers

Add a space after \n in the IFS variable, then remove that space again:

IFS="$(printf '\n ')" && IFS="${IFS% }"
#IFS="$(printf '\n ')" && IFS="${IFS%?}"
printf '%s' "$IFS" | od -A n -c 
like image 97
larz Avatar answered Dec 08 '25 16:12

larz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!