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 ... – phemmer Jun 23 '18 at 22:57
  • 1
    I prefer type -a to which – glenn jackman Jun 24 '18 at 00:56
  • @glennjackman ah yes I forgot about type -a, why is it better than which tho? – Alexander Mills Jun 24 '18 at 05: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 05:36
  • I think type -f is what I am looking for, since that suppresses bash function results – Alexander Mills Jun 24 '18 at 05:40
  • @glennjackman feel free to check out this related question https://unix.stackexchange.com/questions/451542/installing-command-line-tool-if-its-not-in-the-path – Alexander Mills Jun 24 '18 at 06:09

1 Answers1

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
Hauke Laging
  • 90,279