如何將bash的cmd存在變數中 - Linux

By Lucy
at 2015-06-06T16:12
at 2015-06-06T16:12
Table of Contents
ps ux | grep 'ssh xxx' | grep -v 'grep'
ps ux | grep 'ssh xxx' | grep -v 'grep' | awk '{print $2};'
kill `ps ux | grep 'ssh xxx' | grep -v 'grep' | awk '{print $2};'`
想做類似上面的事
我想把第一行的指令存成變數 第2,3行就不用copy&paste
試了一陣子還是無法成功
簡化到後來 發現
cmd="ps ux | grep 'vim'"
$cmd
這樣的2行就無法成功
但是
cmd="ps ux"
$cmd
這樣是可以的
目前的猜測是pipeline造成的
| 是bash的語法,但$cmd解析後就丟給了ps ps認不出 |
請問怎麼寫才能避免最前面3行的copy&paste呢?
--
ps ux | grep 'ssh xxx' | grep -v 'grep' | awk '{print $2};'
kill `ps ux | grep 'ssh xxx' | grep -v 'grep' | awk '{print $2};'`
想做類似上面的事
我想把第一行的指令存成變數 第2,3行就不用copy&paste
試了一陣子還是無法成功
簡化到後來 發現
cmd="ps ux | grep 'vim'"
$cmd
這樣的2行就無法成功
但是
cmd="ps ux"
$cmd
這樣是可以的
目前的猜測是pipeline造成的
| 是bash的語法,但$cmd解析後就丟給了ps ps認不出 |
請問怎麼寫才能避免最前面3行的copy&paste呢?
--
Tags:
Linux
All Comments

By Mia
at 2015-06-08T04:45
at 2015-06-08T04:45

By Valerie
at 2015-06-10T02:56
at 2015-06-10T02:56

By Gilbert
at 2015-06-12T09:32
at 2015-06-12T09:32

By Agnes
at 2015-06-13T07:10
at 2015-06-13T07:10

By Lucy
at 2015-06-15T06:24
at 2015-06-15T06:24

By Todd Johnson
at 2015-06-19T21:59
at 2015-06-19T21:59

By Hedwig
at 2015-06-21T17:53
at 2015-06-21T17:53

By Sierra Rose
at 2015-06-25T04:23
at 2015-06-25T04:23

By Jack
at 2015-06-27T09:52
at 2015-06-27T09:52
Related Posts
如何自動暫停指定的硬碟當不被使用的時候

By Xanthe
at 2015-06-06T00:00
at 2015-06-06T00:00
自動分配檔案並執行(送流量)

By Caitlin
at 2015-06-04T22:49
at 2015-06-04T22:49
Fedora 22 Released

By Olive
at 2015-06-04T00:03
at 2015-06-04T00:03
Fedora 22 Released

By Olivia
at 2015-06-03T23:46
at 2015-06-03T23:46
PowerShell終於要支援SSH,跨Linux和Win

By Rae
at 2015-06-03T20:16
at 2015-06-03T20:16