Not working as expected wit xargs

閲覧: 19 回
最初の未読メッセージにスキップ

jason404

未読、
2021/06/28 23:12:012021/06/28
To: niceeditor
I have been using fzf as a way to select filenames to pipe to ne using xargs, but I get unexpected results. It doesn't seem to have to do with fzf itself, as it happens with whatever I use to pipe to it.

eg.

$ printf ".bashrc\n.bash_aliases\n.hosts\n" | xargs -I {} ne -- {}

This will open the three files in ne, but it also opens an empty file with no filename. This doesn't happen with vim, so I don't think I am doing anything wrong.

Is there anything I can do to stop this from happening?  I find fzf is a useful way to select files.

jason404

未読、
2021/06/28 23:18:032021/06/28
To: niceeditor
Actually, what happens is that it opens an empty file for every actual file it opens.

Jamie Lentin

未読、
2021/06/29 4:09:032021/06/29
To: nicee...@googlegroups.com
On 2021-06-29 4:18, jason404 wrote:
> Actually, what happens is that it opens an empty file for every actual
> file it opens.

That's to do with your xargs formulation (I've not worked out how), but
could you do the fzf equivalent of:

printf ".bashrc\0.bash_aliases\0.hosts\0" | xargs -0 ne

...which only opens one empty file at the start?

As for that first empty file, ne is seeing that stdin isn't a terminal
and assigning a buffer to it[0], in case you were piping something to
it. You can fix that with "-o":

printf ".bashrc\0.bash_aliases\0.hosts\0" | xargs -o -0 ne

Cheers,

[0]
https://github.com/vigna/ne/blob/87f5272787387fbd06f28a2c83ecf428e3687529/src/ne.c#L360-L371

>
> On Tuesday, 29 June 2021 at 04:12:01 UTC+1 jason404 wrote:
>
>> I have been using fzf as a way to select filenames to pipe to ne
>> using xargs, but I get unexpected results. It doesn't seem to have
>> to do with fzf itself, as it happens with whatever I use to pipe to
>> it.
>>
>> eg.
>>
>> $ printf ".bashrc\n.bash_aliases\n.hosts\n" | xargs -I {} ne -- {}
>>
>> This will open the three files in ne, but it also opens an empty
>> file with no filename. This doesn't happen with vim, so I don't
>> think I am doing anything wrong.
>>
>> Is there anything I can do to stop this from happening? I find fzf
>> is a useful way to select files.
>
> --
> You received this message because you are subscribed to the Google
> Groups "niceeditor" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to niceeditor+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/niceeditor/b283a550-a18d-43cf-98d9-055f7c5d7c05n%40googlegroups.com
> [1].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/niceeditor/b283a550-a18d-43cf-98d9-055f7c5d7c05n%40googlegroups.com?utm_medium=email&utm_source=footer

jason404

未読、
2021/06/29 8:54:342021/06/29
To: niceeditor
fzf wasn't causing the multiple files, so just the `-o` has solved the problem. Cheers.

todd_...@unc.edu

未読、
2021/06/29 15:43:012021/06/29
To: niceeditor
Unfortunately, only fairly recent versions of `xargs` supports the `-o` option, but if yours does, that's the right answer.

You can test for it like in this bash function:
```
neg ()
{
    local d="${1:-.}";
    shift;
    local otty=--open-tty;
    xargs --open-tty echo < /dev/null > /dev/null 2>&1 || otty='';
    local binext=(ico jpg jpeg png gif bmp tiff mp4 mkv avi mov mpg vob h264 m4v mpg mpeg rm mp3 aac wav flac ogg mka wma pdf doc docx xls xlsm xlsx pps ppt odt ods o so pyc exe com bin jar dll class z zip tgz gz xz rar 7z tar iso rpm pkg deb msi);
    binext="$(IFS='|'; echo "${binext[*]/%/\\}")";
    findg "$d" "$@" -type f \! -iregex '.*\.\('${binext}')$' -print0 | LANG=C sort -f -V -z | xargs $otty -s 2000000 -0 ${EDITOR:-ne}
}
```
... but then you'll need the `findg` function. (`findg` finds all the files that aren't under `.git` and might be text-ish files.)
```
findg ()
{
    local -a parms=("$@");
    local -a search=();
    local -a opts=();
    local default_action='-print';
    local n;
    if [ ! -d "${parms[0]}" ]; then
        parms=("." "${parms[@]}");
    fi;
    while [ -d "${parms[0]}" ]; do
        search=("${search[@]}" "${parms[0]}");
        parms=("${parms[@]:1}");
    done;
    for n in "${parms[@]}";
    do
        if [[ "${n}" =~ ^-(delete|exec|execdir|fls|(|f)print(|0|f)|ls|ok|okdir|quit)$ ]]; then
            default_action='';
        fi;
    done;
    while [[ "${parms[0]}" =~ ^-(d|daystart|depth|follow|help|ignore_readdir_race|maxdepth|mindepth|mount|noignore_readdir_race|noleaf|regextype|version|warn|nowarn|xautofs|xdev)$ ]]; do
        if [[ "${parms[0]}" =~ ^-(maxdepth|mindepth|regextype)$ ]]; then
            n=2;
        else
            n=1;
        fi;
        opts=("${opts[@]}" "${parms[@]:0:$n}");
        parms=("${parms[@]:$n}");
    done;
    [ -n "$default_action" ] && parms=("${parms[@]}" $default_action);
    [ "${findgd:-0}" = "1" ] && echo find "${search[@]}" "${opts[@]}" \( -name .git -prune \) -o \( "${parms[@]}" \)'
' 1>&2;
    find "${search[@]}" "${opts[@]}" \( -name .git -prune \) -o \( "${parms[@]}" \)
}
```
全員に返信
投稿者に返信
転送
新着メール 0 件