Chinaunix

标题: 在命令行和脚本里面取值不一致 [打印本页]

作者: damoa    时间: 2010-07-06 16:16
标题: 在命令行和脚本里面取值不一致
本帖最后由 damoa 于 2010-07-06 16:19 编辑

小弟写了个脚本,发现有一个情况很奇怪
当我在命令行的时候
/bin/ps -e |/bin/grep proftpd |/bin/grep -v grep | /usr/bin/awk '{print $1}'
2437
当我把命令写入脚本

#!/bin/bash
pid=`/bin/ps -e|/bin/grep proftpd|/bin/grep -v grep|/usr/bin/awk '{print $1}'`
echo $pid

./proftpd   
2437 2602 2603 2605 2606


怎么会有这么多个值啊。想不通,求大大们帮忙解答下
/bin/ps -e|/bin/grep proftpd
2437 ?        00:00:00 proftpd
作者: BangBull    时间: 2010-07-06 16:18
回复 1# damoa


    贴上  /bin/ps -e|/bin/grep proftpd    这个执行的结果
作者: damoa    时间: 2010-07-06 16:18
本帖最后由 damoa 于 2010-07-06 16:19 编辑

/bin/ps -e|/bin/grep proftpd
2437 ?        00:00:00 proftpd
作者: lkk2003rty    时间: 2010-07-06 16:24
管道的问题吧。。。。
作者: BangBull    时间: 2010-07-06 16:25
  1. nagios:root 16:23:57 ~ # /bin/ps -e|/bin/grep gpm|/bin/grep -v grep|/usr/bin/awk '{print $1}'
  2. 2486
  3. nagios:root 16:24:00 ~ # sh s.sh
  4. 2486
  5. nagios:root 16:24:01 ~ # cat s.sh
  6. #!/bin/bash
  7. pid=`/bin/ps -e|/bin/grep gpm|/bin/grep -v grep|/usr/bin/awk '{print $1}'`
  8. echo $pid
  9. nagios:root 16:24:04 ~ # /bin/ps -e|/bin/grep gpm
  10. 2486 ?        00:00:00 gpm
  11. nagios:root 16:24:09 ~ #
复制代码
回复 3# damoa


    没有吧 !?
作者: BangBull    时间: 2010-07-06 16:26
我是说,代码应该没问题
作者: damoa    时间: 2010-07-06 16:27
重建一个文件。。就没这情况了。太神奇了
作者: BangBull    时间: 2010-07-06 16:28

作者: damoa    时间: 2010-07-06 16:47
我好想明白为什么会多个值了。脚本里面$1和awk里面的$1取值是否一样的?
作者: justlooks    时间: 2010-07-06 16:48
直接 pidof不完了么
作者: mgqw    时间: 2010-07-06 17:30
pgrep更方便,pidof还需要超级用户
作者: merlin852    时间: 2010-07-06 19:36
你脚本取什么名字不好,为什么要叫proftpd这个啊

名字取的太帅了,没办法啊




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