I'm working on a shell script (using Bash) for Linux systems.
As part of this script I'm using awk to get the percentage of packets dropped from a ping request. This works fine when executed from the terminal shown below:
test=`ping gnu.org -c10 | tail -2`; echo $test | awk '{printf $6 "\b"}' This gets the percentage of packets dropped and echos it using the backspace escape character to show just "0" rather tan "0%" - this is as the value is used later on as an integer value.
Inside the script, the code looks like this:
#Pings the gateway ten times and stores the result local pingResults=`ping -c10 $gateway | tail -2` #Uses awk to get the percentage of packets lost using \b to remove the % local packetsLost=`echo $pingResults | awk '{printf $6 "\b"}'` I return the value from packetsLost and echo it and it gives "0%", totally ignoring the \b
Could somebody please explain why this is happening inside the shell script and how I can remedy this?
I understand there are alternatives such as using cut but I'd like to keep the line as simple and efficient as possible.
gsubthe%character off in awk instead of playing printing escape tricks?printf("%s", substr($6, 1, length($6)-1)).