如何在bash中找到一个文件(即要点)?

时间:2022-04-15 07:23:22

I want to source a gist into my bash shell, how do I do this in one line? In other words, I do not want to create an intermediate file.

我想在我的bash shell中找到一个要点,我如何在一行中做到这一点?换句话说,我不想创建一个中间文件。

I tried this, but it fails to source the remote file:

我尝试了一下,但是没有找到远程文件的来源:

source <(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)

Running on Mac OSX 10.9.

在Mac OSX 10.9上运行。

Thanks in advance.

提前谢谢。

3 个解决方案

#1


1  

Apple ships an ancient version of Bash, Bash 3.2; Bash 4 was released 5 years ago. Here are a few possible ways to work around this:

苹果发布了一个古老版本的Bash, Bash 3.2;Bash 4是5年前发布的。下面是一些解决这个问题的方法:

  1. Install MacPorts, Homebrew, or pkgsrc and install Bash via one of them; or just build and install it yourself from source. Remember to add your newly installed bash to /etc/shells so you can set it as your shell, then go to "System Preferences > Users and Groups", click the lock to supply your password so you can make changes, then right click (two-finger click/control click) on your user to choose "Advanced Options..." and change your shell there.
  2. 安装MacPorts、Homebrew或pkgsrc,并通过其中之一安装Bash;或者直接从源代码构建和安装它。记得添加你的新安装的bash /etc/shells所以你可以设置它作为您的shell,然后去“系统设置>用户和组”,单击锁定供应你的密码,这样你就可以进行更改,然后点击右键(两手指点击/控制点击)用户选择“高级选项…”,改变您的shell。
  3. If you must be compatible with the 7 year old Bash shipped on OS X, you could just save the file and source it from there. Here's an example Bash function to make that easier:

    如果您必须与OS X上发布的已有7年历史的Bash兼容,那么您可以将文件保存下来,并从那里获取源代码。这里有一个示例Bash函数,使之更容易:

    function curlsource() {
        f=$(mktemp -t curlsource)
        curl -o "$f" -s -L "$1"
        source "$f"
        rm -f "$f"
    }
    curlsource https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
    
  4. If you absolutely must avoid even creating a temporary file, and must run on ancient versions of Bash, the best you can do is read into a string and eval the result. I tried to emulate the effect of source <(cmd) by creating a FIFO (named pipe), piping the output of cmd into it, and reading it with source, but got nothing. It turns out, taking a look at the source for Bash 3.2, source simply reads the whole file into a string, and it checks the file size before doing so. A FIFO returns a size of 0 when you stat it, so source happily allocates a string of length 1 (for the trailing null), reads 0 bytes into it, and returns success. So, since source is just reading the whole file into a string and then evaluating that, you can just do the same:

    如果您绝对必须避免创建临时文件,并且必须在Bash的旧版本上运行,那么您所能做的最好的事情就是将结果读入一个字符串并对其求值。我试图通过创建一个FIFO(命名管道)来模拟source <(cmd)的效果,将cmd的输出导入其中,并使用source读取它,但一无所获。事实证明,在查看Bash 3.2的源代码时,源代码只是将整个文件读入一个字符串,并在这样做之前检查文件的大小。当您对FIFO进行统计时,它会返回0的大小,因此源代码会很高兴地分配长度为1的字符串(对于末尾的null),在其中读取0字节,并返回success。因此,因为source只是将整个文件读入一个字符串,然后对其进行评估,你可以这样做:

    eval "$(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)"
    

#2


1  

OS X still ships bash 3.2 by default; in that version, the source command does not seem to work properly with process substitutions, as can be demonstrated with a simple test:

OS X默认仍然附带bash 3.2;在那个版本中,源命令似乎不能很好地处理过程替换,这可以用一个简单的测试来演示:

$ source <(echo FOO=5)
$ echo $FOO

$

The same source command does, however, work in bash 4.1 or later (I don't have a 4.0 installation to test, and the release notes seem to be silent on the matter.)

但是,同样的源命令在bash 4.1或更高版本中工作(我没有一个4.0的安装来测试,而且发布说明似乎对这个问题保持沉默)。

#3


-2  

This will work on any version of bash and without any file creation.

这将在任何版本的bash上工作,并且不创建任何文件。

source <<< "$(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)"

#1


1  

Apple ships an ancient version of Bash, Bash 3.2; Bash 4 was released 5 years ago. Here are a few possible ways to work around this:

苹果发布了一个古老版本的Bash, Bash 3.2;Bash 4是5年前发布的。下面是一些解决这个问题的方法:

  1. Install MacPorts, Homebrew, or pkgsrc and install Bash via one of them; or just build and install it yourself from source. Remember to add your newly installed bash to /etc/shells so you can set it as your shell, then go to "System Preferences > Users and Groups", click the lock to supply your password so you can make changes, then right click (two-finger click/control click) on your user to choose "Advanced Options..." and change your shell there.
  2. 安装MacPorts、Homebrew或pkgsrc,并通过其中之一安装Bash;或者直接从源代码构建和安装它。记得添加你的新安装的bash /etc/shells所以你可以设置它作为您的shell,然后去“系统设置>用户和组”,单击锁定供应你的密码,这样你就可以进行更改,然后点击右键(两手指点击/控制点击)用户选择“高级选项…”,改变您的shell。
  3. If you must be compatible with the 7 year old Bash shipped on OS X, you could just save the file and source it from there. Here's an example Bash function to make that easier:

    如果您必须与OS X上发布的已有7年历史的Bash兼容,那么您可以将文件保存下来,并从那里获取源代码。这里有一个示例Bash函数,使之更容易:

    function curlsource() {
        f=$(mktemp -t curlsource)
        curl -o "$f" -s -L "$1"
        source "$f"
        rm -f "$f"
    }
    curlsource https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
    
  4. If you absolutely must avoid even creating a temporary file, and must run on ancient versions of Bash, the best you can do is read into a string and eval the result. I tried to emulate the effect of source <(cmd) by creating a FIFO (named pipe), piping the output of cmd into it, and reading it with source, but got nothing. It turns out, taking a look at the source for Bash 3.2, source simply reads the whole file into a string, and it checks the file size before doing so. A FIFO returns a size of 0 when you stat it, so source happily allocates a string of length 1 (for the trailing null), reads 0 bytes into it, and returns success. So, since source is just reading the whole file into a string and then evaluating that, you can just do the same:

    如果您绝对必须避免创建临时文件,并且必须在Bash的旧版本上运行,那么您所能做的最好的事情就是将结果读入一个字符串并对其求值。我试图通过创建一个FIFO(命名管道)来模拟source <(cmd)的效果,将cmd的输出导入其中,并使用source读取它,但一无所获。事实证明,在查看Bash 3.2的源代码时,源代码只是将整个文件读入一个字符串,并在这样做之前检查文件的大小。当您对FIFO进行统计时,它会返回0的大小,因此源代码会很高兴地分配长度为1的字符串(对于末尾的null),在其中读取0字节,并返回success。因此,因为source只是将整个文件读入一个字符串,然后对其进行评估,你可以这样做:

    eval "$(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)"
    

#2


1  

OS X still ships bash 3.2 by default; in that version, the source command does not seem to work properly with process substitutions, as can be demonstrated with a simple test:

OS X默认仍然附带bash 3.2;在那个版本中,源命令似乎不能很好地处理过程替换,这可以用一个简单的测试来演示:

$ source <(echo FOO=5)
$ echo $FOO

$

The same source command does, however, work in bash 4.1 or later (I don't have a 4.0 installation to test, and the release notes seem to be silent on the matter.)

但是,同样的源命令在bash 4.1或更高版本中工作(我没有一个4.0的安装来测试,而且发布说明似乎对这个问题保持沉默)。

#3


-2  

This will work on any version of bash and without any file creation.

这将在任何版本的bash上工作,并且不创建任何文件。

source <<< "$(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)"