0

I have a function that worked fine in bash

function cfwipe() {
    local space_guid=`cf space --guid  $1`
    cf t -s $1
    for a in `cf curl /v2/spaces/$space_guid/apps | jq -r .resources[].entity.name`; do cf delete -r -f $a; done
    for a in `cf curl /v2/spaces/$space_guid/service_instances | jq -r .resources[].entity.name`; do cf ds -f $a;done
    for a in `cf curl /v2/user_provided_service_instances?q=space_guid:$space_guid | jq -r .resources[].entity.name`; do cf ds -f $a;done
}

I copied the function to .zshrc file and added it to autoload

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit cfwipe
    compinit
fi

However when I try to run it - despite starting the execution it fails and repeats in console the same line

no matches found: .resources[].entity.name
...

What is the problem? Even with spellcheck fixes it doesn't work

cfwipe() {
    space_guid=$(cf space --guid  "$1")
    cf t -s "$1"
    for a in $(cf curl /v2/spaces/"$space_guid"/apps | jq -r .resources[].entity.name); do cf delete -r -f "$a"; done
    for a in $(cf curl /v2/spaces/"$space_guid"/service_instances | jq -r .resources[].entity.name); do cf ds -f "$a";done
    for a in $(cf curl /v2/user_provided_service_instances?q=space_guid:"$space_guid" | jq -r .resources[].entity.name); do cf ds -f "$a";done
}
2
  • I see a lot of unquoted variables and stuff like that. Start by fixing the problems pointed out by shellcheck.net if that doesn't help, update the answer. Commented Apr 29, 2020 at 8:52
  • @Socowi but it worked with Iterm because I switched to zsh Commented Apr 29, 2020 at 11:30

2 Answers 2

2

You didn't quote the jq filters. The [] in the filters makes both shells recognize the strings as patterns and attempt to apply pathname generation. By default, bash treats an unmatched pattern as literal text. zsh, on the other hand, treats an unmatched pattern as an error.

If you had a file named .resources.entity.name in the current directory, both shells would use that as the result of pathname generation, altering the string you meant to pass as an argument to jq.

If you don't want the shell to process a string, quote it.

... | jq -r '.resources[].entity.name'

Unfortunately, shellcheck cannot flag this as an error, because it doesn't know what an arbitrary command expects its arguments to be: maybe the unquoted pattern was intentional.


As an aside, either shell's default behavior can be changed. To make bash behave like zsh, use

shopt -s failglob

To make zsh behave like bash, use

setoption NO_NOMATCH

(I'm not sure why there is an option named NOMATCH enabled by default, rather than an option named MATCH that is disabled by default.)

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

Comments

0

I set NO_NOMATCH, escape jq '.[]'. Doesn't work for me at all. Bash works fine.

Comments

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.