Skip to main content
deleted 1 character in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

I'd like to know if it's possible to write a POSIX compliant script that builds a find command from it'sits arguments.

The way to do it for Bash (and zsh) is explained in this other SE article but it's using facilities that are Bashbash (or zsh) specific.

The original question was to be able to build a find command like this:

find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

The answer is with the following Bash script code:

findany() {
    local args=('-iname' "*$1*")
    shift

    while [ "$#" -gt 0 ]; do
        args+=('-o' '-iname' "*$1*")
        shift
    done

    find . "${args[@]}"
}

Is it possible to do something similar in a POSIX-compliant script?

I'd like to know if it's possible to write a POSIX compliant script that builds a find command from it's arguments.

The way to do it for Bash (and zsh) is explained in this other SE article but it's using facilities that are Bash (or zsh) specific.

The original question was to be able to build a find command like this:

find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

The answer is with the following Bash script code:

findany() {
    local args=('-iname' "*$1*")
    shift

    while [ "$#" -gt 0 ]; do
        args+=('-o' '-iname' "*$1*")
        shift
    done

    find . "${args[@]}"
}

Is it possible to do something similar in a POSIX-compliant script?

I'd like to know if it's possible to write a POSIX compliant script that builds a find command from its arguments.

The way to do it for Bash (and zsh) is explained in this other SE article but it's using facilities that are bash (or zsh) specific.

The original question was to be able to build a find command like this:

find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

The answer is with the following Bash script code:

findany() {
    local args=('-iname' "*$1*")
    shift

    while [ "$#" -gt 0 ]; do
        args+=('-o' '-iname' "*$1*")
        shift
    done

    find . "${args[@]}"
}

Is it possible to do something similar in a POSIX-compliant script?

Source Link
PRouleau
  • 305
  • 1
  • 7

Build find command from arguments in a POSIX compliant shell script

I'd like to know if it's possible to write a POSIX compliant script that builds a find command from it's arguments.

The way to do it for Bash (and zsh) is explained in this other SE article but it's using facilities that are Bash (or zsh) specific.

The original question was to be able to build a find command like this:

find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

The answer is with the following Bash script code:

findany() {
    local args=('-iname' "*$1*")
    shift

    while [ "$#" -gt 0 ]; do
        args+=('-o' '-iname' "*$1*")
        shift
    done

    find . "${args[@]}"
}

Is it possible to do something similar in a POSIX-compliant script?