how to delete the trailing carriage return in bash
| July 30th, 2009So im making a web interface for troubleshooting mpls links for some customers, Im writing the entire thing in bash, and things were going pretty well till yesterday. ( i started on saturday). You see the first page currently has the name one customer currently with a small input form next to and a troubleshoot button next to it, Now for a Mpls vpn customer there are various ways to provide last mile connectivity, im not talking about physical connectivity but the logical PE-CE connectivity, anyways after a few basic steps of automated telnet i will end up on the last mile PE and from there i want to do a source destination ping using vrf , somewhat like this
ping vrf CUSTOMER 20.20.20.2 source 20.20.20.1 repeat 3
or i use a source interface to ping , which is pretty much the same as source ip, now 20.20.20.2 is a variable which we get when somebody inputs it into the webpage. so actually when i send the commands the ip and interface are basically variables sent across. Which actually works fine. the issue comes up when the last mile connectivity has been temporarly given on a gre tunnel , Now in this case i need to breakdance with cat/grep/sed and get the source and destination ip of the tunnel and then send across those variables to check if that connectivity exists. I know its anal, if the tunnel ip’s dont ping then 99% the tunnel destination from the tunnel source won’t ping, but i wanted to follow proper steps in this automated troubleshooting system i was building. so the command sent across is
ping $destination source $source repeat 3
But it did’nt work, since i was pushing all output to a txt file , i could see that command being run on the router was
ping $destination
thats it ! , now since im no programmer, it took me some time to understand whats going on, i figured there’s a new line character being added to the $destination variable which i take after certain cat /grep/cut commands done on the output file. so i added this to the if else command after reading about the tr -d ‘\n’ command somewhere on the internet
$dest=$(echo $dest | tr -d ‘\n’)
Still didnt work, couldnt figure it out, but then i remembered that there is also a carriage return character \r , what if that also gets added in the variable, so next step was
$dest=$(echo $dest | tr -d ‘\r\n’)
and this time it worked, perfectly fine.
I feel like i will be never able to understand programming completely, because such kind of problems take me two days, but once i get the solution i dont care, im happy and i move forward. scripting/programming for network troubleshooting, is much more rewarding that plain network troubleshooting.
Got some questions about kerrighed, but comments arent allowed under that entry,mail me please, thanks very much!