a problem when using shell handles conversion of integer IP to the standard IP

created at 06-13-2022 views: 3

background

There are a batch of ip data sources, and the specified fields need to be cleaned out, but the source ip is of digital type;

Use the conversion method:

function int_to_ip
{
N=$1
declare -i H1="$N & 0x000000ff"
declare -i H2="($N & 0x0000ff00) >> 8"
declare -i L1="($N & 0x00ff0000) >> 16"
declare -i L2="($N & 0xff000000) >> 24"
echo "$L2.$L1.$H2.$H1"
}

But the incoming parameter must be an integer;

Wrong action:

echo $line |  awk '{print $2}' | int_to_ip

Report exception:

./int_to_ip.sh: line 4: declare: '0' & 0x000000ff: syntax error: operand expected (error token is ""0" & 0x000000ff")

reason

The cut data is with "";

solution

echo $line | awk '{print $2}'|sed 's/\"//g'
created at:06-13-2022
edited at: 06-13-2022: