linux - shell 脚本 : read the output to run the next command

标签 linux bash shell unix scripting

我正在尝试运行它,但仍然没有得到输出。不知道我是不是错了。 首先脚本应该登录到服务器
第二条命令是“netstat -tulpun | grep -i port(端口和服务器列表在同一个文件中)
3rd 它可能在这里得到 3 到 4 个输出,但我只需要 25 个 PID 输出而不是其他的

#!/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  

更新所需的输出:

=====
服务器1
端口 1311

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

=====
服务器2
端口 1311

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

=====
服务器3
端口 8000

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

最佳答案

不需要的输出行可能是由于所需的 PID 出现在 ps -ef 输出(PPID、CMD)的其他列中或作为另一个 PID 的一部分。将 ps -ef 替换为 ps -fp% 以纠正它。

关于linux - shell 脚本 : read the output to run the next command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959343/

相关文章:

php - UTF-8贯穿始终

linux - CentOS7如何保留程序?

linux - 如何将 bash/awk 命令放入变量(不是命令的输出)

linux - tar exclude 在 bash 脚本中不起作用

shell - 如何在导入 CSV 文件时向 Solr 添加额外字段?

linux - 我正在尝试在程序集中创建一个三角形的点,但它不起作用

C - Linux - 自定义内核模块来迭代进程的子进程会破坏内核日志和计算机

regex - Bash 正则表达式匹配不起作用 - 转义所有特殊字符

linux - 每次运行脚本后变量加1

python - 如何让我的别名从 postfix 服务器调用 shell 脚本来调用 python 程序?