我试图理解为什么 bash 脚本似乎在某个if
语句处停止执行。我在脚本中添加了 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.