0

This is a weird one - it should work.

I have this file /home/foo/waldo.sh

#!/usr/bin/env bash

waldo(){

   if [[ -z $(command -v waldo) ]]; then
       npm install -g '@oresoftware/waldo' || {
         return 1;
      }
   fi

   command waldo "$@"
}


export -f waldo;

if I source the file with:

. "/home/foo/waldo.sh";

and then run

waldo

I get:

No command 'waldo' found, did you mean:
 Command 'aldo' from package 'aldo' (universe)
waldo: command not found

when I run $(which waldo), it's empty, nothing there.

however, when I run type waldo, I get:

waldo is a function
waldo () 
{ 
    if [[ -z $(command -v waldo) ]]; then
        npm install -g '@oresoftware/waldo' || { 
            return 1
        };
    fi;
    command waldo "$@"
}

anyone know why it's not being sourced or whatever?

  • 1
    You're using command substitution too much. There's no need for $(command ...) or $(which ...). Use the exit status of the command you're calling instead of checking for output. E.G.: if ! command -v waldo >/dev/null; then ... – Patrick Jun 23 '18 at 22:57
  • 1
    I prefer type -a to which – glenn jackman Jun 24 '18 at 0:56
  • @glennjackman ah yes I forgot about type -a, why is it better than which tho? – Alexander Mills Jun 24 '18 at 5:35
  • In this case, I need a command that looks up to see what's in the PATH only, ignoring shell functions, that should be somewhat obvious from the question right. – Alexander Mills Jun 24 '18 at 5:36
  • I think type -f is what I am looking for, since that suppresses bash function results – Alexander Mills Jun 24 '18 at 5:40
3

The function is found and executed but it contains

command waldo

That's what the error message refers to.

You could handle this problem in the function:

if hash waldo >/dev/null 2>&1; then
    command waldo "$@"
else
    echo 'ERROR: ...'
fi
  • ok thanks! then why did the if condition evaluate to false then? in other words, why didn't command -v waldo evaluate to empty string? – Alexander Mills Jun 23 '18 at 22:09
  • I see the update that should help, but I think in the if check to see if it's installed already, I need to use something other than command -v waldo ? – Alexander Mills Jun 23 '18 at 22:11
  • maybe which waldo instead? – Alexander Mills Jun 23 '18 at 22:11
  • @AlexanderMills Is the npm install executed (set -x)? Does it put a binary waldo in the PATH? – Hauke Laging Jun 23 '18 at 22:26
  • yeah it does put "waldo" in the $PATH – Alexander Mills Jun 23 '18 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.