rr0ss0rr
rr0ss0rr

Reputation: 153

Why is my awk print not showing up on the terminal

I have the following script which does a "which -a" on a command then a "ls -l" to let me know if it's a link or not .. ie "grep" since I have gnu commands installed (Mac with iTerm).

#!/usr/bin/env bash
which -a $1 | xargs -I{} ls -l "{}" \
| awk '{for (i = 1; i < 9; i++) $i = ""; sub(/^ */, ""); print}'

When I run this from the script "test grep" I receive no output, but when I run it via "bash -x test grep" I receive the following:

bash -x test grep
+ which -a grep
+ xargs '-I{}' ls -l '{}'
+ awk '{for (i = 1; i < 9; i++) $i = ""; sub(/^ */, ""); print}'
/usr/local/bin/grep -> ../Cellar/grep/3.1/bin/grep
/usr/bin/grep

The last 2 lines is what I'm looking to display. Thought this would be easier to do ;-) .. I also tried appending the pipe thinking printf would fix the issue:

| while read path
do
   printf "%s\n" "$path"
done

Thanks and .. Is there a better way to get what I need?

Upvotes: 1

Views: 640

Answers (4)

rr0ss0rr
rr0ss0rr

Reputation: 153

Took some suggestions and came up with the following: The prt-underline is just a fancy printf function. I decided not to go with readline since the ultimate command resolution may be unfamiliar to me and I only deal with regular files .. so does't handle every situation but in the end gives me the output I was looking for. Thanks for all the help.

llt ()
{
    case $(type -t -- "$1") in
        function)
            prt-underline "Function";
            declare -f "$1"
        ;;
        alias)
            prt-underline "Alias";
            alias "$1" | awk '{sub(/^alias /, ""); print}'
        ;;
        keyword)
            prt-underline "Reserved Keyword"
        ;;
        builtin)
            prt-underline "Builtin Command"
        ;;
        *)

        ;;
    esac;
    which "$1" &> /dev/null;
    if [[ $? = 0 ]]; then
        prt-underline "File";
        for i in $(which -a $1);
        do
            stat "$i" | awk 'NR==1{sub(/^  File: /, ""); print}';
        done;
    fi
}

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295639

Consider the following shell function:

cmdsrc() {
  local cmd_file cmd_file_realpath
  case $(type -t -- "$1") in
    file) cmd_file=$(type -P -- "$1")
          if [[ -L "$cmd_file" ]]; then
            echo "$cmd_file is a symlink" >&2
          elif [[ -f "$cmd_file" ]]; then
            echo "$cmd_file is a regular file" >&2
          else
            echo "$cmd_file is not a symlink or a regular file" >&2
          fi
          cmd_file_realpath=$(readlink -- "$cmd_file") || return
          if [[ $cmd_file_realpath != "$cmd_file" ]]; then
            echo "...the real location of the executable is $cmd_file_realpath" >&2
          fi
          ;;
    *)    echo "$1 is not a file at all: $(type -- "$1")" >&2 ;;
  esac
}

...used as such:

$ cmdsrc apt
/usr/bin/apt is a symlink
...the real location of the executable is /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/apt
$ cmdsrc ls
/bin/ls is a regular file
$ cmdsrc alias
alias is not a file at all: alias is a shell builtin

Upvotes: 1

rr0ss0rr
rr0ss0rr

Reputation: 153

Thanks for the never naming a script "test" .. old habits are hard to break (I came from a non-unix background.

I ended with the following

for i in $(which -a $1)
do
   stat $i | awk NR==1{'$1 = ""; sub(/^ */, ""); print}'
done

or simpler

for i in $(which -a $1)
do
  stat -c %N "$i"
done

Upvotes: 1

melpomene
melpomene

Reputation: 85827

The problem is that you named your script test.

If you want to run a command that's not in your PATH, you need to specify the directory it's in, e.g. ./test.

You're not getting an error for trying to run test because there is a built-in bash command called test that is used instead. For extra confusion, the standard test produces no output.

In conclusion:

  • Use ./ to run scripts in the current directory.
  • Never call your test programs test.

Upvotes: 1

Related Questions