I have written a simple script that grabs an ip in CDIR format from openstack. The line looks like this:
cidr="10.62.149.62/27"
I used awk to isolate just the IP, and then used awk one last time to store "27" in a variable called POOL_SIZE
.
#!/bin/bash
NETWORK=$1
POOL=$( openstack subnet show --insecure $NETWORK -f shell|grep -w "cidr"|awk -F '"' '{print $2}'|awk -F '/' '{print $2}')
Now, I want to be able to subtract 1 from the 27 to get the value 26 and store in into a different variable.
POOL_SIZE=$(( $POOL - 1 ))
echo $POOL_SIZE
However, when I execute this script, I get this output:
- 1 ")syntax error: invalid arithmetic operator (error token is "
If I echo $POOL
I get this output:
27
Therefore, I think that there is some character after "27" like a \\r
or \\n
that is causing the problem.
So, I want to be able to get rid of those extra characters and subtract 1 from 27.
How would I fix this?
Once you have the network, you can strip the address with
prefix_size=${cidr#*/}
and stript any trailing carriage return output by openstack
with
prefix_size=${prefix_size%$'\r'}
$'\\r'
is a bash extension, so in other more limited POSIX shells you can use tr
instead.
prefix_size=$(echo "$prefix_size" | tr -d '\r')
Or, save a carriage return to a variable once to use later:
cr=$(printf '\r')
...
prefix_size=${prefix_size%$cr}
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.