如何在case语句中使用流程替换而不出现语法错误?

TCZ8

我在/etc/init.d/myfile中将脚本加载为服务

当我尝试启动服务时出现错误

/etc/init.d/myservice: 21: /etc/init.d/myservice: Syntax error: "(" unexpected

问题似乎与进程替换<(在源命令中。我在其他脚本中使用它毫无问题地从我的主配置文件中提取变量有关,但是在case语句中我不知道如何使它起作用。

myservice包含:

#!/bin/sh
#/etc/init.d/myservice

### BEGIN INIT INFO
# Provides:          myservice
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: my service
# Description:       Start the myservice service
### END INIT INFO

case "$1" in
  start)
    # start processes
        # Import the following variables from config.conf: cfgfile, dir, bindir
        source <(grep myservice /opt/mysoftware/config.conf | grep -oP '.*(?= #)')
        if [ -f $cfgfile ]
        then
            echo "Starting myservice"
            /usr/bin/screen -U -d -m $bindir/myscript.sh $cfgfile
        else
            echo "myservice could not start because the file $cfgfile is missing"
        fi
    ;;
  stop)
    # kill processes
    echo "Stopping myservice"
    screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
    ;;
  restart)
    # kill and restart processes
    /etc/init.d/myservice stop
    /etc/init.d/myservice start
    ;;
  *)
    echo "Usage: /etc/init.d/myservice {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

文件config.conf是变量声明的列表,带有简短说明和使用它们的脚本名称。我使用grep过滤器仅获取给定脚本所需的变量。

看起来像这样:

var1=value # path to tmp folder   myservice
var2=value # log file name        myservice script1.sh script2.sh
var3=value # prefix for log file  script1.sh script2.sh

注意:在将其转换为开始使用配置文件而不是硬编码值之前,该服务运行良好。

谢谢你。

马克·普洛特尼克

Bash,ksh93,zsh和其他最近的Shell支持进程替换(<(command)语法),但这是非标准扩展。Dash(/bin/sh在Ubuntu系统上)不支持它,而bash在调用时也不支持/bin/sh

如果有可用的bash,请将脚本的第一行更改为例如#!/bin/bash

[在可挂载文件系统的目录中具有bash的系统上,例如/usr/local/bin在某些系统上,可能需要在启动服务之前确保文件系统可用。]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章