Chinaunix
标题:
help,为什么在shell中取不到值
[打印本页]
作者:
heavensend
时间:
2013-02-17 11:58
标题:
help,为什么在shell中取不到值
#!/bin/bash
path=$1
exec_log_file=/opt/tools/mem_restart.log
mem_port=`cat ${path}/config.php |grep Memport |awk '{print $3}'|sed "s/\;//"`
mem_pid=`ps -ef |grep memcache|grep ${mem_port}|grep -v grep|awk '{print $2}'`
复制代码
这个mem_pid总是得不到,运行的结果为
+ path=/var/www/html
+ exec_log_file=/opt/tools/mem_restart.log
+ cat /var/www/html/config.php
+ grep Memport
+ awk {print $3}
+ sed s/\;//
+ mem_port=11211
+ ps -ef
+ grep memcache
+ grep 11211
+ grep -v grep
+ awk {print $2}
+ mem_pid=
复制代码
实际上,我自己试过,发现问题是在grep ${mem_port}|这里,加了这个就取不到值。
但是想不明白,请高手不吝赐教
作者:
关阴月飞
时间:
2013-02-17 12:43
ps -ef |grep memcache 运行的结果是啥? 贴出来看看吧。。。。
作者:
heavensend
时间:
2013-02-17 12:55
root 10821 10785 0 12:55 pts/0 00:00:00 grep --color=auto memcache
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
就是因为在命令行下没有问题,所以才觉得很费解
# ps -ef |grep memcache |grep 11211 |grep -v grep |awk '{print $2}'
15692
复制代码
作者:
dn833
时间:
2013-02-17 14:39
netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/{print gensub(/\/.*/,"",1,$NF)}'
复制代码
这样能取到memcache的pid吗?
作者:
关阴月飞
时间:
2013-02-17 15:01
回复
4#
heavensend
LZ, 我这里测试木有发现有问题呢。。。。。。。。。。。
[root@test]# cat test.sh
#!/bin/bash
path=$1
exec_log_file=/opt/tools/mem_restart.log
mem_port=`cat ${path}/config.php |grep Memport |awk '{print $3}'|sed "s/\;//"`
mem_pid=`ps -ef |grep memcache |grep ${mem_port}|grep -v grep|awk '{print $2}'`
[root@ test]# bash -x test.sh .
+ path=.
+ exec_log_file=/opt/tools/mem_restart.log
++ cat ./config.php
++ grep Memport
++ awk '{print $3}'
++ sed 's/\;//'
+ mem_port=12000
++ ps -ef
++ grep memcache
++ grep 12000
++ grep -v grep
++ awk '{print $2}'
+ mem_pid=1717
[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
awk: line 2: function gensub never defined
复制代码
作者:
dn833
时间:
2013-02-17 15:54
netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/'
复制代码
这样能出啥结果呢???还有就是LZ你啥系统啊???
作者:
heavensend
时间:
2013-02-17 16:35
回复
11#
dn833
netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/'
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 15692/memcached
复制代码
more /etc/issue
Ubuntu 10.04 LTS \n \l
复制代码
我这里都是用的ubuntu
作者:
dn833
时间:
2013-02-17 16:54
netstat -tpln|awk '$4~/11211$/&&$NF~/memcached$/{gsub(/\/sshd/,"");print $NF}'
复制代码
我在ubuntu虚拟机下测试成功的,LZ你自己试试看吧。因为ubuntu默认用的是mawk,这里大多数人用的都是gawk。
作者:
heavensend
时间:
2013-02-17 17:14
回复
13#
dn833
用您提供的方法确实可以了
export mem_port=11211
netstat -tpln|awk '$4~/'${mem_port}'$/&&$NF~/memcached$/{gsub(/\/sshd/,"");print $NF}'|cut -d\/ -f1
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