1

Note I'm including the functions for context of what's happening. The ibmcloud CLI is not itself relevant, it just has a prompt which is the problem.

I'm using zsh. I have the following functions:

kcu() {
  kcu_match=`kcg | grep $1 | awk '{print $2}'`
  if [[ -z $kcu_match ]]; then
    echo No match for context \"$1\"
  else
    kc use-context $kcu_match
    # Do a trivial operation to determine login
    k get namespace 1>/dev/null 2> >(kcu-iferror $kcu_match)
  fi
}
# version for the first screenshot..
# kcu-iferror() {
#  while read line; do
#    # if we are here we have an error
#    slm-login-ic $1
#    # first line is enough
#    break
#  done
# }

#version for the second screenshot.. kcu-iferror() { login=0 while read line; do login=1 # shows the output (in this case 2 lines but doesn't matter) echo "line: $line" break done echo "value: $login" if [[ $login -eq 1 ]]; then slm-login-ic $1 fi }

slm-login-ic() { echo Log in to $1.. echo ">> " ibmcloud login -a cloud.ibm.com -r us-south -g Default --sso ibmcloud login -a cloud.ibm.com -r us-south -g Default --sso echo ">> " ibmcloud ks cluster config --cluster $1 ibmcloud ks cluster config --cluster $1 }

If I call slm-login-ic my-cluster-id directly works just fine.

However, if I run kcu my-cluster-id I have a problem: the error output is being used as an input to the ibmcloud prompt "Open the URL in the default browser?" and submitting it (screen shot below).

How do I stop this from happening so the user can enter Y?
Bonus question: how would I change the "error" to a Y so the user doesn't need to answer the prompt?

enter image description here

Update - the story continues

So, after some experimentation it appears that the real issue is that the 2> >(something) is non-blocking. Which means that my CLI command has already closed but the error redirection (which takes some time) is still outputting things and generally making things messy.

Question then becomes: how do I make a redirect of STDERR to be blocking? Thanks

Update - down to the final "non-blocking" blocker

By letting the entire error message loop through, I can at least stop an actual line from posting to the ibmcloud Y/n prompt. But I can't stop it from posting entirely. The whole crux of this is how to a) have the parent shell/process wait and b) have the error object stop being spit out - in this case - to a prompt in the child shell/process. Thanks

I have tried all of the following:

      k get namespace -A 2>&1 1>/dev/null | kcu-iferror $kcu_match
    { k get namespace -A } 1>/dev/null 2> >(kcu-iferror $kcu_match)
      k get namespace -A 2>&1 1>/dev/null | kcu-iferror $kcu_match
    { k get namespace -A } 2>&1 1>/dev/null | kcu-iferror $kcu_match

enter image description here

  • no -y call in ibmcloud login ? or explicit browser selection like a -browser firefox? – Archemar Apr 22 '22 at 15:03
  • Helpful to know but doesn't address the issue I'm having specifically – Oliver Williams Apr 22 '22 at 15:55
  • What did your picture show? Please don't post images of text – Chris Davies Apr 22 '22 at 22:44
  • 2
    In general this would be hard, but since you also want to discard stdout, do k get namespace 2>&1 1>/dev/null | kcu-iferror $kcu_match. Running a pipeline waits for both (or all) parts to complete. (It's actually process substitution >(...) that is nonblocking, it doesn't matter which fd it is.) – dave_thompson_085 Apr 23 '22 at 02:58
  • @dave_thompson_085 I tried and updated my post. I believe the pipe did wait till the process finished, but I'm still left with the remains of the STDERR "file" (an EOF) being spit into the ibmcloud prompt unwelcomely. – Oliver Williams Apr 23 '22 at 10:03

1 Answers1

0

As stated in the manual, the solution is to run the process, from which you want to redirect the output, as a job in the current shell:

    # Do a trivial operation to determine login
    { k get namespace } 1>/dev/null 2> >(kcu-iferror $kcu_match)

This will cause the parent shell to wait for the process to complete.