Search code examples
shellifconfig

Capture output of ifconfig eth0 inet6 add and assign to variable


I have a script to add IPv6s. But I need to figure out how to assign the output of the command to a variable and then use it further in the code. This is what I have

 ret=$(/sbin/ifconfig eth0 inet6 add $IPV6PROXYADD)
 if [ "$ret" ];
 then
  returnflag="error"
  echo "$ret" >> "/root/mypath/ipv6/ipadderror.log"
 fi

But the script would output the result to screen instead of assigning it to the variable. What am I doing wrong here?


Solution

  • I decided to add another answer rather than modify my original. This approach keeps most of your original code (though it is not my style :-)

    ret=$( /sbin/ifconfig eth0 inet6 add $IPV6PROXYADD 2>&1 )
    if [ $? -ne 0 ];
    then
      returnflag="error"
      echo "$ret" >> "/root/mypath/ipv6/ipadderror.log"
    fi
    

    I kept the return code check $? rather than looking at the content of variable ret.