0

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.

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

output:

/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

before I changed the code to the above, the if condition looked like that:

if [[ ${f##*/} == values* ]]
then
    ...

But this doesn't work either.

Thanks for you suggestions...

1
  • 3
    Your question contradicts itself: You tag it as bash, but in the title you write "sh". For bash, [[ ${f##*/} == values* ]] would make sense; for sh obviously not. Also, does not work well and doesn't work either is anecdotal evidence, but not a problem description. Commented Apr 14, 2021 at 10:46

1 Answer 1

1

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.

You could use the shell case statement to match a variable against a glob:

case "$f" in
  */values*)
    echo "$f"
    ;;
esac

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

(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.)

Sign up to request clarification or add additional context in comments.

2 Comments

in your 1st example you close brackets /values) but not open. Is that purposely?
@I.zv, that is the case syntax, see help case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.