Chinaunix

标题: help,为什么在shell中取不到值 [打印本页]

作者: heavensend    时间: 2013-02-17 11:58
标题: help,为什么在shell中取不到值
  1. #!/bin/bash
  2. path=$1
  3. exec_log_file=/opt/tools/mem_restart.log
  4. mem_port=`cat ${path}/config.php |grep Memport |awk '{print $3}'|sed "s/\;//"`
  5. mem_pid=`ps -ef |grep memcache|grep ${mem_port}|grep -v grep|awk '{print $2}'`
复制代码
这个mem_pid总是得不到,运行的结果为
  1. + path=/var/www/html
  2. + exec_log_file=/opt/tools/mem_restart.log
  3. + cat /var/www/html/config.php
  4. + grep Memport
  5. + awk {print $3}
  6. + sed s/\;//
  7. + mem_port=11211
  8. + ps -ef
  9. + grep memcache
  10. + grep 11211
  11. + grep -v grep
  12. + awk {print $2}
  13. + mem_pid=
复制代码
实际上,我自己试过,发现问题是在grep ${mem_port}|这里,加了这个就取不到值。
但是想不明白,请高手不吝赐教
作者: 关阴月飞    时间: 2013-02-17 12:43
ps -ef |grep memcache  运行的结果是啥? 贴出来看看吧。。。。
作者: heavensend    时间: 2013-02-17 12:55
  1. root     10821 10785  0 12:55 pts/0    00:00:00 grep --color=auto memcache
  2. nobody   15692     1  0 Jan27 ?        00:15:54 memcached -d -m 1024 -p 11211 -u nobody -c 1024 -l 127.0.0.1
复制代码
就是这样
作者: heavensend    时间: 2013-02-17 13:00
就是因为在命令行下没有问题,所以才觉得很费解
  1. # ps -ef |grep memcache |grep 11211 |grep -v grep |awk '{print $2}'
  2. 15692
复制代码

作者: dn833    时间: 2013-02-17 14:39
  1. netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/{print gensub(/\/.*/,"",1,$NF)}'
复制代码
这样能取到memcache的pid吗?
作者: 关阴月飞    时间: 2013-02-17 15:01
回复 4# heavensend
LZ, 我这里测试木有发现有问题呢。。。。。。。。。。。

  1. [root@test]# cat test.sh
  2. #!/bin/bash
  3. path=$1
  4. exec_log_file=/opt/tools/mem_restart.log
  5. mem_port=`cat ${path}/config.php |grep Memport |awk '{print $3}'|sed "s/\;//"`
  6. mem_pid=`ps -ef |grep memcache |grep ${mem_port}|grep -v grep|awk '{print $2}'`
  7. [root@ test]# bash -x test.sh .
  8. + path=.
  9. + exec_log_file=/opt/tools/mem_restart.log
  10. ++ cat ./config.php
  11. ++ grep Memport
  12. ++ awk '{print $3}'
  13. ++ sed 's/\;//'
  14. + mem_port=12000
  15. ++ ps -ef
  16. ++ grep memcache
  17. ++ grep 12000
  18. ++ grep -v grep
  19. ++ awk '{print $2}'
  20. + mem_pid=1717
  21. [root@ test]#
复制代码

作者: heavensend    时间: 2013-02-17 15:25
恩,我刚刚换了台机器发现没问题,这下更想不通了。
作者: heavensend    时间: 2013-02-17 15:26
比较一下两台的内核版本
2.6.35-22 --取不到值的
2.6.32-38 --取得到值的
作者: heavensend    时间: 2013-02-17 15:38
回复 5# dn833
sorry,您的awk语句我没看明白,但是报了语法错误
作者: heavensend    时间: 2013-02-17 15:39
回复 5# dn833
  1. awk: line 2: function gensub never defined
复制代码

作者: dn833    时间: 2013-02-17 15:54
  1. netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/'
复制代码
这样能出啥结果呢???还有就是LZ你啥系统啊???
作者: heavensend    时间: 2013-02-17 16:35
回复 11# dn833
  1. netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/'
  2. tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      15692/memcached
复制代码
  1. more /etc/issue
  2. Ubuntu 10.04 LTS \n \l
复制代码
我这里都是用的ubuntu
作者: dn833    时间: 2013-02-17 16:54
  1. netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/{gsub(/\/sshd/,"");print $NF}'
复制代码
我在ubuntu虚拟机下测试成功的,LZ你自己试试看吧。因为ubuntu默认用的是mawk,这里大多数人用的都是gawk。
作者: heavensend    时间: 2013-02-17 17:14
回复 13# dn833
用您提供的方法确实可以了
  1. export mem_port=11211
  2. netstat -tpln|awk '$4~/'${mem_port}'$/&&$NF~/memcached$/{gsub(/\/sshd/,"");print $NF}'|cut -d\/ -f1
  3. 15692
复制代码
非常感谢!
只是我之前的方法为什么在特定主机上得不到结果让人费解
作者: 圣枪骑兵    时间: 2013-02-18 15:07
本帖最后由 圣枪骑兵 于 2013-02-18 15:10 编辑

grep               
作者: zhsh87    时间: 2013-02-18 15:36
我建议可以这样排查一下,把下面的这句话
mem_pid=`ps -ef |grep memcache|grep ${mem_port}|grep -v grep|awk '{print $2}'`
分成几种情况
mem_pid=`ps -ef |grep memcache|grep ${mem_port}|grep -v grep|awk '{print $2}'`
mem_pid=`ps -ef |grep memcache|grep ${mem_port}|grep -v grep`
mem_pid=`ps -ef |grep memcache|grep ${mem_port}`
mem_pid=`ps -ef |grep memcache`

执行的时候加-x,看每种情况的区别,希望能帮到你
作者: heavensend    时间: 2013-02-19 11:06
回复 16# zhsh87
3Q,我查过,问题是在|grep ${mem_port}这里。


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2