简体   繁体   中英

assign value to external variable inside Linux awk

I need your helps about assigning value to external variable inside awk command.Please check the following script

interfaceName=$1
exitStatus=0

ip -o link show | awk '{ if($2=="'$interfaceName'") $exitStatus=1}'

if(exitStatus==0) then
  exit 0

else 
  exit 1
fi

The script takes network interface name as argument then check the available interfaces which match with the taken. if find matched interface it, exits with 1 else with 0.The problem is that i cannot change the external variable exitStatus inside awk in the following line

awk '{ if($2=="'$interfaceName'") $exitStatus=1}'

How to assign value to it in awk ?

You can make awk return an exit status to the shell:

ip -o link show | awk '{ if($2=="'$interfaceName'") exit 1}'

if [ $? -eq 1 ]; then
    # TODO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM