Re:GNU Coreutils Gotchas
by dvh on Monday November 30, 2015 @08:19AM
(#10649196)
for me, it's details like this:
I even reported it decade ago but nobody cares.
[ Parent
| on HN ]
Discussed at http://lists.gnu.org/archive/html/coreutils/2011-01/msg00080...
Summary of that is you can filter using xargs like:
get_file_paths | xargs basename -a
No point having two ways to do something,
especially when there are caveats about enabling stdin processingWhy should basename accepts its argument on stdin? It doesn't act as a filter, processing a stream of data, but rather as a plain function.
> Why should basename accepts its argument on stdin?
Because a lot of people have use for a filter version
> It doesn't act as a filter, processing a stream of data, but rather as a plain function.
That's the problem with a lot commands, some work as filters, some as simple commands, and other do both. The reasoning on the decision is confusing.
[edit: yes, combining with xargs will often work]
Re:GNU Coreutils Gotchas
by philjackson on Monday November 30, 2015 @08:42AM
(#10649285)
Why do you expect basename to do something with stdin?
I ended up writing my own version of basename and dirname that acted as both filters and commands. Its actually not that hard given the functions you have (might give it another go given the whole pledge thing just to see how it would work). URL versions of these commands are also very useful and not too hard to program in perl.
A practical issue: how many paths should basename accept on stdin? If more than 1, how should they be delimited?
Re:GNU Coreutils Gotchas
by andrewchambers on Monday November 30, 2015 @02:56PM
(#10651354)
obviously with a newline like every other unix tool. sort, uniq, grep etc.
But paths can contain newlines.
Re:GNU Coreutils Gotchas
by scrollaway on Thursday December 03, 2015 @03:52PM
(#10672544)
Most tools that deal with paths delimit them on newlines and support a -z or -0 flag to delimit with \0.
$ echo ${PWD##*/}
etc
use your shell to its full powerif you need to pipe it in, you should
basename `echo $PWD`uhm, what?
Can you give an example where I have a file (a.txt) of 100 file names and need the basename of each? I'm pretty sure dvh just used echo to show a simple example, not an actual use.
Re:GNU Coreutils Gotchas
by erdbeerkaese on Monday November 30, 2015 @09:55AM
(#10649661)
Another way:
while read line ; do basename "$line" ; done < a.txtYeah, I think I prefer yours. Probably more robust wrt filenames-with-whitespace too.
Both solutions although nice and helpful pretty much illustrate why I ended up writing a filter capable version.
How about this?
for f in $(cat a.txt); do basename "$f"; done1. Broken by filenames that contain whitespace.
2. The shell will expand * and ? (maybe others) in the filenames
xargs basename -a < a.txt