在 shellscript 裡面找出 process 的數目 - Linux

Table of Contents


我寫了一支 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



想請問為何有這樣的差異呢 ?

--

All Comments

Sierra Rose avatarSierra Rose2015-05-22
pross substitution是用sub shell實現的,多一層sh
Erin avatarErin2015-05-28
這兩個結果是不是放反啦?應該有echo的才會失去換行字元
Olive avatarOlive2015-06-01
數目?那要加 | wc -l 啊