News for hackers. Stuff that matters.
Re:Bash Pitfalls by hyperpape on Sunday August 16, 2015 @02:38PM (#10069807)
Referencing the other thread on shell scripting currently on the front page (https://news.ycombinator.com/item?id=10068668), some of these examples show ways in which bash is actually quite verbose:

    # POSIX
    for i in *.mp3; do
        [ -e "$i" ] || continue
        some_command "$i"
    done

    # HYPOTHETICAL SYNTAX 1
    map some_command *.mp3

    # HYPOTHETICAL SYNTAX 2
    some_command *.mp3
It's hard to imagine how to create simple syntax for operations like that while accomodating other syntactic requirements (strings without quotations, pipelines, etc), but I dream about a shell language that lets me do things like the above.
Re:Bash Pitfalls by lisivka on Sunday August 16, 2015 @06:56PM (#10070578)

  map() {
    local COMMAND="${1:?Argument is required: command to execute, e.g. "echo". Example: \"map echo *\".}"
    shift 1
    local I
    for I in "$@"
    do
        [ -e "$I" ] || continue
        $COMMAND "$I"
    done
  }
Re:Bash Pitfalls by hyperpape on Sunday August 16, 2015 @07:56PM (#10070748)
Neat! I'm curious: did you come up with this on the spot, or is this actually something you use?
Re:Bash Pitfalls by lisivka on Wednesday August 19, 2015 @01:36PM (#10087129)
I just typed it in window.
Re:Bash Pitfalls by rando289 on Sunday August 16, 2015 @03:26PM (#10069964)
The first command is distinguishing between broken links, and changing the -e to different letters, it can differentiate between 23 different attributes, and act differently depending on each. Your hypothetical examples do not do not have that ability. And, to do what your commands would imply, usually you really can do "some_command *.mp3", it's not hypothetical!
Re:Bash Pitfalls by hyperpape on Sunday August 16, 2015 @03:47PM (#10070026)
There are two points here. You say you can do "some_command *.mp3"...true if either a) your command is safe to run with garbage input or b) you know that you have >= one file and no whitespace in filenames. Both of these are often reasonable assumptions, but I want a general purpose solution that doesn't have more edge cases than it has to.

The mental model of bash is "this text expands to this text like so, which means x haappens." The model I want is "I'm gonna do x to each of these files."

As for the 23 commands, my attitude is that this is usually either ~19 too many, or infinitely too few.

I acknowledge that maybe the way of doing things will start to make sense at some point, but it hasn't happened yet, and I find trying to climb the hill of understanding the shell to be a joyless experience.

Re:Bash Pitfalls by a3n on Sunday August 16, 2015 @03:58PM (#10070056)
> As for the 23 commands, my attitude is that this is usually either ~19 too many, or infinitely too few.

As recently seen on HN: http://www.cs.yale.edu/homes/perlis-alan/quotes.html

11. If you have a procedure with ten parameters, you probably missed some.

Re:Bash Pitfalls by cmma on Sunday August 16, 2015 @08:46PM (#10070881)
> You say you can do "some_command * .mp3"...true if either a) your command is safe to run with garbage input or b) you know that you have >= one file and no whitespace in filenames.

Globbing preserves whitespace. It's up to the target function to discard it, by forgetting to quote arguments.

    example1() {
        [ $# -eq 0 ] && return
        echo "$1"
        shift
        example1 "$@"
    }

    example2() {
        for arg in "$@"; do
            echo "$arg"
        done
    }

    touch 1 "2 3" 4
    example1 *
    example2 *

    # both examples print:
    #     1
    #     2 3
    #     4
Re:Bash Pitfalls by fluidcruft on Monday August 17, 2015 @08:28PM (#10076965)
"xargs" is basically what you're thinking of as "map", and "find" is more powerful than simple globbing.

    find . -type f -name \*.mp3 -print0 | xargs -n 1 -0 some_command