Shell Script : read the output to run the next com

2019-07-26 10:46发布

问题:

I am trying to run this but still not getting the output. Don't know were I am wrong. First the script should login to the server
2nd command is "netstat -tulpun | grep -i port (port & server list is in the same file)
3rd It may get 3 to 4 output here but I need only 25 PID output not the others

#!/bin/bash  
while read -r -u10 server port line  
do  
echo ========== server: "$server" port: "$port" ==========  
   ssh $line "netstat -tulpunt | grep -E \"\b$port\b\"" | awk '{print $7}' | grep '/' | awk -F '/' '{print $1}' | xargs -I % bash -c 'echo Port % && ps -ef | grep % && echo ' | grep -v grep  
done 10< demo  

Updated required output

Server1
Port 1311
root 8063 8062 0 2014 ? 00:08:06 /opt/dm_cd -run

=====
Server2
Port 1311
root 6844 6843 0 2014 ? 00:20:22 /etc/bin/linux/ds -run

=====
Server3
Port 8000
applmgr 1505 4215 0 2014 ? 00:05:44 /app/Apache/bin/httpd -d

回答1:

The unwanted output lines are probably due to the needed PID appearing in other columns of the ps -ef output (PPID, CMD) or as part of another PID. Replace ps -ef by ps -fp% to rectify it.