Sunday 15 July 2012

bash - if-then-else based on command output -



bash - if-then-else based on command output -

i using netstat command info on networks. want set status here on protocol fetched. if it's tcp, need print different columns if it's udp.

below trying do, doesn't work. please suggest , advise if there doing wrong:

if [$(netstat -anputw | awk '{print $1}')=="tcp"] netstat -anputw | awk '{print $1,",",$4"}' >> $home/mylog/connections_$hostname.csv elif [$(netstat -anputw | awk '{print $1}')=="udp"] netstat -anputw | awk '{print $5,",",$7}' >> $home/mylog/connections_$hostname.csv fi

i don't know you're trying achieve, think netstat returns list rather string, hence comparing output against string pointless. have loop it. seek following

#!/bin/bash output=$(netstat -anputw | awk '{print $1}'); line in $output if [[ $line == "tcp" ]] ; echo "tcp!" elif [[ $line == "udp" ]] ; echo "udp!" fi done

bash if-statement

No comments:

Post a Comment