Shell Script : read the output to run the next command

点点圈 提交于 2020-02-05 08:17:09

问题


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.



来源:https://stackoverflow.com/questions/27959343/shell-script-read-the-output-to-run-the-next-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!