问题描述
使用busybox和ash运行基于图像的高山:
Running an image based alpine with busybox and ash:
/mnt/builddir/code/src/main/helm # busybox | head -1
BusyBox v1.31.1 () multi-call binary.
我写了一个sh脚本,该脚本仅在文件名以前缀"values"开头但以"if"开头的情况下打印文件名.条件不能很好地工作.这是我的脚本:
I wrote an sh script that prints file's names only if they start with prefix "values", but something with the "if" condition does not work well. This is my script:
for f in ./*
do
echo ${f##*/}
if ${f##*/} == 'values'*; then
echo $f
fi
done
输出:
/mnt/builddir/code/src/main/helm # ./script.sh
Chart.yaml
./script.sh: line 4: Chart.yaml: not found
script.sh
./script.sh: line 4: script.sh: not found
values-feature.yaml
./script.sh: line 4: values-feature.yaml: not found
values-int.yaml
./script.sh: line 4: values-int.yaml: not found
values-prod.yaml
./script.sh: line 4: values-prod.yaml: not found
values-stg.yaml
./script.sh: line 4: values-stg.yaml: not found
values.yaml
./script.sh: line 4: values.yaml: not found
在将代码更改为上面的代码之前,if条件如下所示:
before I changed the code to the above, the if condition looked like that:
if [[ ${f##*/} == values* ]]
then
...
但这也不起作用.
感谢您的建议...
推荐答案
此脚本中存在两个明显的问题:在您的 if
行中,您没有调用 test (1)而是直接尝试运行每个文件;而 test (1) ==
运算符仅进行精确的字符串比较,而不进行glob或regex匹配.
There are two obvious problems in this script: in your if
line you're not invoking test(1) but instead are directly trying to run each file; and the test(1) ==
operator only does exact string comparisons and not glob or regex matches.
您可以使用shell case
语句将变量与glob匹配:
You could use the shell case
statement to match a variable against a glob:
case "$f" in
*/values*)
echo "$f"
;;
esac
但是shell for
语句可以遍历glob扩展,这通常是一个更简单的设置:
But the shell for
statement can iterate over a glob expansion, and that will be a generally simpler setup:
for f in values*; do
echo "$f"
done
(这完全不是特定于Docker的,并且我希望您在直接在主机上运行脚本时会遇到非常类似的错误.您可能会发现,无需将Docker作为主机来开发和调试脚本要容易得多.与您要修复的代码之间的隔离层.)
(This is not at all specific to Docker, and I'd expect you'd get very similar errors running the script directly on the host. You might find it much easier to develop and debug the script without having Docker as an isolation layer between you and the code you're trying to fix.)
这篇关于Shell脚本(busybox)在docker容器上无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!