从脚本在后台执行 shell 命令
问题描述:
如果命令在字符串中,如何在 bash 脚本中在后台执行 shell 命令?
how can I execute a shell command in the background from within a bash script, if the command is in a string?
例如:
#!/bin/bash
cmd="nohup mycommand";
other_cmd="nohup othercommand";
"$cmd &";
"$othercmd &";
这不起作用——我该怎么做?
this does not work -- how can I do this?
答
去掉引号
$cmd &
$othercmd &
例如:
nicholas@nick-win7 /tmp
$ cat test
#!/bin/bash
cmd="ls -la"
$cmd &
nicholas@nick-win7 /tmp
$ ./test
nicholas@nick-win7 /tmp
$ total 6
drwxrwxrwt+ 1 nicholas root 0 2010-09-10 20:44 .
drwxr-xr-x+ 1 nicholas root 4096 2010-09-10 14:40 ..
-rwxrwxrwx 1 nicholas None 35 2010-09-10 20:44 test
-rwxr-xr-x 1 nicholas None 41 2010-09-10 20:43 test~