gpt4 book ai didi

linux - 在 Linux 中扩展

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:52 27 4
gpt4 key购买 nike

我正在阅读下面的教程

http://linuxcommand.org/lc3_lts0080.php

关于双引号:

Double Quotes

The first type of quoting we will look at is double quotes. If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (back- quote). This means that word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed, but parameter expansion, arithmetic expansion, and command substitution are still carried out.

关于单引号:

Single Quotes

If you need to suppress all expansions, you use single quotes.

它确实用 ls 解释了为什么只有 *.txt 不工作 "*.txt"'*。 txt'

 $ ls -l *.txt
-rw-r--r-- 1 Gaurav None 8893 Apr 17 06:25 BigText.txt
-rwxr-xr-x 1 Gaurav None 54376 Apr 18 12:32 File.txt
-rw-r--r-- 1 Gaurav None 371 Apr 23 21:04 Filelist.txt
-rwxr-xr-x 1 Gaurav None 54386 Apr 18 12:28 FileOld.txt
-rw-r--r-- 1 Gaurav None 1163 Apr 17 06:19 Read.txt
-rw-r--r-- 1 Gaurav None 651 Apr 24 06:28 temp.txt


$ ls -l "*.txt"
ls: cannot access *.txt: No such file or directory
$ ls -l '*.txt'
ls: cannot access *.txt: No such file or directory

我的问题是,使用 find 或 grep 为什么 "*.txt", '*.txt' 都提供扩展:(事实上 * .txt 没有为名称提供任何扩展,与 ls)

不同

例如

$ find . -name *.txt
find: paths must precede expression: File.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


$ find . -name "*.txt"
./BigText.txt
./File.txt
./Filelist.txt
./FileOld.txt
./Read.txt
./sample/BigText.txt
./sample/File.txt
./sample/FileOld.txt
./sample/Read.txt
./sample/temp.txt
./temp.txt


$ find . -name '*.txt'
./BigText.txt
./File.txt
./Filelist.txt
./FileOld.txt
./Read.txt
./sample/BigText.txt
./sample/File.txt
./sample/FileOld.txt
./sample/Read.txt
./sample/temp.txt
./temp.txt

最佳答案

find自己扩展* – 它不需要 shell 来完成。

您需要了解在 shell 中键入命令(或 shell 从脚本执行命令时)会发生什么:

首先,所有的扩展都是由shell完成的,比如变量扩展(把$foo变成foo的值),波浪符扩展(把~变成主目录)路径)和通配符(将 * 转换为所有匹配的文件)。

然后,在完成所有扩展后,扩展后的值作为命令执行。

当您调用 find 时,您实际上希望 shell 扩展 *你传递给find作为模式的一部分,因为 find命令解释 *本身并将其用作搜索模式(如 find <dir> -name '*.foo' )。

关于linux - 在 Linux 中扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com