繁体   English   中英

-bash:cmd:找不到命令

[英]-bash: cmd: command not found

我在/etc/init.d/unicorn中有这个

#!/bin/bash
# /etc/init.d/unicorn

# ### BEGIN INIT INFO
# chkconfig: 2345 95 016
# processname: unicorn
# dscription: Stop/start unicorn
### END INIT INFO

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

cmd() {
    cd /vagrant
    unicorn -p 3000 -D
}

# Start the service
start() {
    su - vagrant -c cmd
}

### main logic ###
case "$1" in
    start)
        start
        ;;
    *)
        echo $"Usage: $0 {start}"
        exit 1
esac

exit 0

我正在尝试在我的本地rails文件夹中启动独角兽。 我觉得这个命令应该工作:

su - vagrant -c cmd

并且无法找出原因。

我想您给人的印象是,该脚本中定义的shell函数cmd应该在早先启动该脚本的shell中可用。

这是错误的,除非你source d该脚本(这是不寻常的脚本/etc/init.d/ )。 当启动脚本(而不是source )时,然后启动第二个执行脚本的进程。 Shell函数的所有定义(如cmd )仅在该Shell脚本内有效,并随进程终止。

如果你真正想要的cmd可用,你将不得不source脚本unicorn

source /etc/init.d/unicorn

但是, su命令仍然无法调用该Shell函数,因为它只能调用可执行文件,并且可以使用exec()开始exec() ,因此它们必须是文件。 Shell函数不是。

要解决此问题,请将函数内联到su调用的shell中:

start() {
    su - vagrant -c 'cd /vagrant && unicorn -p 3000 -D'
}

对于首先找出原因,工具很有帮助:

$ shellcheck unicorn

In unicorn line 19:
    su - vagrant -c cmd
                    ^-- SC2033: Shell functions can't be passed to external commands.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM