我试图匹配给定的字符串,并将其与/bin/sh脚本中的包版本相匹配:
if test "x$version" = "x"; then
version="latest";
info "Version parameter not defined, assuming latest";
else
info "Version parameter defined: $version";
info "Matching version to package version"
case "$version" in
[^4.0.]*)
$package_version='1.0.1'
;;
[^4.1.]*)
$package_version='1.1.1'
;;
[^4.2.]*)
$package_version='1.2.6'
;;
*)
critical "Unable to match requested version to package version"
exit 1
;;
esac
fi但是,当我运行它时,我会得到一个错误:
23:38:47 +0000 INFO: Version parameter defined: 4.0.0
23:38:47 +0000 INFO: Matching Puppet version to puppet-agent package version (See http://docs.puppetlabs.com/puppet/latest/reference/about_agent.html for more details)
23:38:47 +0000 CRIT: Unable to match requested puppet version to puppet-agent version - Check http://docs.puppetlabs.com/puppet/latest/reference/about_agent.html
23:38:47 +0000 CRIT: Please file a bug report at https://github.com/petems/puppet-install-shell/
23:38:47 +0000 CRIT:
23:38:47 +0000 CRIT: Version: 4.0.0在脚本的另一部分中,我使用了与我相同的正则表达式:,它似乎在那里工作:
if test "$version" = 'latest'; then
apt-get install -y puppet-common puppet
else
case "$version" in
[^2.7.]*)
info "2.7.* Puppet deb package tied to Facter < 2.0.0, specifying Facter 1.7.4"
apt-get install -y puppet-common=$version-1puppetlabs1 puppet=$version-1puppetlabs1 facter=1.7.4-1puppetlabs1 --force-yes
;;
*)
apt-get install -y puppet-common=$version-1puppetlabs1 puppet=$version-1puppetlabs1 --force-yes
;;
esac
fi我遗漏了什么?
脚本的完整版本如下:agent.sh
发布于 2015-11-27 00:16:53
case ... esac使用的是(glob-style) ,而不是正则表达式(虽然两者关系遥远,但有根本的区别)。- To get true regex matching in a `sh` script, you'd have to use `expr` with `:`, though it's probably not needed here.
<prefix>*分支中使用case分支总是与整个参数匹配-不需要锚定(模式不支持)。- As an aside, what you're attempting would not even work for prefix matching as a _regex_. E.g., `[^4.0.]` is the same as `[^.04]` - i.e., a _negated_ character _class_: it matches _one_ character if it is neither `.` nor `0` nor `4`.
$。把它们放在一起:
#/bin/sh
if [ "$version" = "" ]; then
version="latest";
info "Version parameter not defined, assuming latest"
else
info "Version parameter defined: $version";
info "Matching version to package version"
case "$version" in
4.0.*)
package_version='1.0.1'
;;
4.1.*)
package_version='1.1.1'
;;
4.2.*)
package_version='1.2.6'
;;
*)
critical "Unable to match requested version to package version"
exit 1
;;
esac
fihttps://stackoverflow.com/questions/33948484
复制相似问题