3 ms·
Referencing the other thread on shell scripting currently on the front page (https://news.ycombinator.com/item?id=10068668 https://news.ycombinator.com/item?id=
by hyperpape 11y ago
Referencing the other thread on shell scripting currently on the front page (https://news.ycombinator.com/item?id=10068668 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.
- rando289 11y agoThe 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!
- hyperpape 11y agoThere 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.
- a3n 11y ago> 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 http://www.cs.yale.edu/homes/perlis-alan/quotes.html 11. If you have a procedure with ten parameters, you probably missed some.
- cmma 11y ago> 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
- lisivka 11y agomap() { 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 }
- fluidcruft 11y ago"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