Skip to main content
added 291 characters in body
Source Link
Chris Davies
  • 128.4k
  • 16
  • 179
  • 324

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -f    # noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

Conveniently you don't need local in this function because the ( … ) already localises the context.

As a final offering, here's the version without a loop

pfind() ( set -f; IFS=:; find $PATH -mindepth 1 -maxdepth 1 -type f -name "$1" 2>/dev/null )

where you have to discard stderr completely in order to avoid outputting errors for directories in $PATH that don't exist

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -f    # noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

Conveniently you don't need local in this function because the ( … ) already localises the context.

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -f    # noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

Conveniently you don't need local in this function because the ( … ) already localises the context.

As a final offering, here's the version without a loop

pfind() ( set -f; IFS=:; find $PATH -mindepth 1 -maxdepth 1 -type f -name "$1" 2>/dev/null )

where you have to discard stderr completely in order to avoid outputting errors for directories in $PATH that don't exist

added 106 characters in body
Source Link
Chris Davies
  • 128.4k
  • 16
  • 179
  • 324

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -of    # noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

Conveniently you don't need local in this function because the ( … ) already localises the context.

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -o noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -f    # noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

Conveniently you don't need local in this function because the ( … ) already localises the context.

Rollback to Revision 2
Source Link
Chris Davies
  • 128.4k
  • 16
  • 179
  • 324

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function which, with shells that support local (most these days) also allows making changes to $IFS/$- local:

pfind() (
    local name="$1" - IFS=: p
    set -o noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find -H "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

(local - needs a sh implementation based on the Almquist shell or recent versions of bash)

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function which, with shells that support local (most these days) also allows making changes to $IFS/$- local:

pfind() (
    local name="$1" - IFS=: p
    set -o noglob
    for p in $PATH
    do
        [ -d "$p" ] && find -H "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'

(local - needs a sh implementation based on the Almquist shell or recent versions of bash)

You could roll a little loop

(
    name='gcc-*'
    IFS=:; for p in $PATH    # Mind filename globbing
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

which can easily be converted to a function

pfind() (
    name="$1"
    set -o noglob
    IFS=:; for p in $PATH
    do
        [ -d "$p" ] && find "$p" -maxdepth 1 -type f -name "$name"
    done
)

pfind 'gcc-*'
added 69 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
No globbing please
Source Link
Chris Davies
  • 128.4k
  • 16
  • 179
  • 324
Loading
Source Link
Chris Davies
  • 128.4k
  • 16
  • 179
  • 324
Loading