구문 오류 없이 Case 문 내에서 프로세스 대체를 사용하는 방법은 무엇입니까?

구문 오류 없이 Case 문 내에서 프로세스 대체를 사용하는 방법은 무엇입니까?

/etc/init.d/myfile에 서비스로 스크립트를 로드했습니다.

서비스를 시작하려고 하면 오류가 발생합니다.

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

문제는 소스 명령의 프로세스 대체 <(에 있는 것 같습니다. 다른 스크립트에서는 문제 없이 사용하여 기본 구성 파일에서 변수를 추출하지만 케이스 문 내부에서는 어떻게 작동하는지 모르겠습니다.

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

참고: 하드코딩된 값 대신 구성 파일을 사용하기 위해 서비스를 변환하기 전에는 서비스가 제대로 작동했습니다.

감사합니다.

답변1

Bash, ksh93, zsh 및 기타 최신 셸은 프로세스 대체( <(command)구문)를 지원하지만 이는 비표준 확장입니다. Dash(Ubuntu 시스템에 있음 /bin/sh)는 이를 지원하지 않으며 호출 시 bash /bin/sh도 지원하지 않습니다.

Bash를 사용할 수 있는 경우 스크립트의 첫 번째 줄을 예를 들어 #!/bin/bash.

[일부 시스템과 같이 마운트 가능한 파일 시스템의 디렉토리에 bash가 있는 시스템에서는 /usr/local/bin서비스가 시작되기 전에 파일 시스템을 사용할 수 있는지 확인해야 할 수도 있습니다.]

답변2

프로세스 대체바시즘, 하지만 당신의셰뱅 라인이다 #!/bin/sh. /bin/shBash 또는 프로세스 대체를 지원하는 다른 쉘이 아닌 한 해당 구문은 실제로 지원되지 않습니다.@MarkPlotnick.

관련 정보