![bash 스크립트는 실행 시 if 문에서 잠깁니다.](https://linux55.com/image/69642/bash%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%8A%94%20%EC%8B%A4%ED%96%89%20%EC%8B%9C%20if%20%EB%AC%B8%EC%97%90%EC%84%9C%20%EC%9E%A0%EA%B9%81%EB%8B%88%EB%8B%A4..png)
if
Bash 스크립트가 특정 명령문에서 실행이 중지 되는 이유를 이해하려고 합니다 . 스크립트에 echo 문을 추가했습니다.
다음과 같은 첫 번째 배치가 있습니다 make.sh
.
#!/bin/bash
export OPENSHIFT_RUNTIME_DIR=${OPENSHIFT_HOMEDIR}/app-root/runtime
export ROOT_DIR=${OPENSHIFT_RUNTIME_DIR} #CARTRIDGE
export LIB_DIR=${ROOT_DIR}/lib
export CONF_DIR=${OPENSHIFT_REPO_DIR}/conf
export DIST_PHP_VER=5.6.11
pushd ${OPENSHIFT_REPO_DIR}/misc
chmod +x make_php
echo 'before source make_php'
source make_php
echo 'before check_all'
check_all
popd
스크립트는 make_php
다음과 같습니다
#!/bin/bash
function install_php() {
...
}
function check_php() {
echo 'entering check php'
if [[ -x $OPENSHIFT_RUNTIME_DIR/bin/php-cgi ]] ; then
echo 'entering check php between if'
if [[ "`$OPENSHIFT_RUNTIME_DIR/bin/php-cgi`" =~ "${DIST_PHP_VER}" ]] ; then
echo 'leaving check php return 0'
return 0
fi
fi
echo "Check PHP ${DIST_PHP_VER} Failed. Start installing"
install_php
}
function check_composer() {
echo 'entering check composer'
...
echo 'leaving check composer'
}
function check_all() {
echo 'entering check all'
check_php
echo 'after check php'
check_composer
echo 'after composer'
}
을 실행하면 ./make.sh
출력은 다음과 같습니다.
before source make_php
entering check all
entering check php
entering check php between if
프롬프트가 표시되지 않습니다. CTRL-C를 눌러야 합니다.
이 문제의 원인은 무엇입니까? 그리고 그것을 해결하는 방법은 무엇입니까?
고쳐 쓰다
$OPENSHIFT_RUNTIME_DIR/bin/으로 이동하여 php-cgi를 실행하면 프로그램이 작동 중지됩니다. 이는 문제를 설명합니다.
답변1
스크립트가 버전 번호를 얻으려고 시도하는 대신 PHP를 애플리케이션으로 실행하는 것 같습니다. 해당 줄을 다음과 같이 변경하면 문제를 해결할 수 있습니다.
if [[ "`$OPENSHIFT_RUNTIME_DIR/bin/php-cgi --version`" =~ "${DIST_PHP_VER}" ]] ; then
테스트 =~
는 정규식 일치이며 bash의 일부입니다. ~에서큰 타격(1)매뉴얼 페이지:
An additional binary operator, =~, is available, with the same prece‐
dence as == and !=. When it is used, the string to the right of the
operator is considered an extended regular expression and matched
accordingly (as in regex(3)). The return value is 0 if the string
matches the pattern, and 1 otherwise. If the regular expression is
syntactically incorrect, the conditional expression's return value is
2. If the shell option nocasematch is enabled, the match is performed
without regard to the case of alphabetic characters. Any part of the
pattern may be quoted to force the quoted portion to be matched as a
string. Bracket expressions in regular expressions must be treated
carefully, since normal quoting characters lose their meanings between
brackets. If the pattern is stored in a shell variable, quoting the
variable expansion forces the entire pattern to be matched as a string.
Substrings matched by parenthesized subexpressions within the regular
expression are saved in the array variable BASH_REMATCH. The element
of BASH_REMATCH with index 0 is the portion of the string matching the
entire regular expression. The element of BASH_REMATCH with index n is
the portion of the string matching the nth parenthesized subexpression.