作者hpps (hpps)
看板Linux
标题[问题] 在 shellscript 里面找出 process 的数目
时间Thu May 21 22:36:24 2015
我写了一支 shellscript,叫做 test.sh,内容是:
#!/bin/bash
var=$(ps aux | grep 'test' )
echo $var
然後我执行的时候输入 ./test.sh
结果输出:
linux 7264 0.0 0.2 5180 1092 pts/0 S+ 22:33 0:00 /bin/bash ./test.sh
linux 7265 0.0 0.1 5180 512 pts/0 S+ 22:33 0:00 /bin/bash ./test.sh
linux 7267 0.0 0.1 4384 800 pts/0 S+ 22:33 0:00 grep test
如果把 shellscript 改成:
#!/bin/bash
ps aux | grep 'test'
会输出:
linux 7271 0.0 0.2 5176 1096 pts/0 S+ 22:36 0:00 /bin/bash ./test.sh
linux 7273 0.0 0.1 4384 800 pts/0 S+ 22:36 0:00 grep test
想请问为何有这样的差异呢 ?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.127.58.99
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Linux/M.1432218987.A.0AF.html
1F:→ kdjf: pross substitution是用sub shell实现的,多一层sh 05/21 23:06
3F:推 goldie: 这两个结果是不是放反啦?应该有echo的才会失去换行字元 05/22 13:08
4F:推 pizzahut: 数目?那要加 | wc -l 啊 05/25 00:45