- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定以下命令:
$(basename "/this-directory-does-not-exist/*.txt" ".txt")
它不仅输出 txt 文件,还输出其他文件。另一方面,如果我将“.txt”更改为“gobble de gook”之类的内容,它会返回:
*.txt
我对它返回其他扩展类型的原因感到困惑。
最佳答案
您的问题并非源于 basename
,而是源于由于缺少引号而无意中使用了 shell 的路径名扩展(通配符)功能:
如果您使用命令替换的结果 ($(...)
) 未加引号:
$ echo $(basename "/this-directory-does-not-exist/*.txt" ".txt")
您有效地执行了以下操作:
$ echo * # unquoted '*' expands to all files and folders in the current dir
因为 basename "/this-directory-does-not-exist/*.txt"".txt"
返回文字 *
(它从文件名 *.txt
;
文件名 pattern *.txt
没有扩展为实际文件名的原因是 shell 留下了不匹配任何未修改的任何匹配模式(默认情况下)。 )
如果您双引号命令替换,问题就会消失:
$ echo "$(basename "/this-directory-does-not-exist/*.txt" ".txt")" # -> *
但是,即使解决了这个问题,您的 basename
命令只有在 glob 扩展到 一个 匹配文件时才能正常工作,因为您使用的语法形式仅支持一个 文件名参数。
GNU basename
和 BSD basename
支持非 POSIX -s
选项,它允许多个文件操作数从中剥离扩展名:
basename -s .txt "/some-dir/*.txt"
假设您使用 bash
,您可以将它们稳健地组合在一起,如下所示:
#!/usr/bin/env bash
names=() # initialize result array
files=( *.txt ) # perform globbing and capture matching paths in an array
# Since the shell by default returns a pattern as-is if there are no matches,
# we test the first array item for existence; if it refers to an existing
# file or dir., we know that at least 1 match was found.
if [[ -e ${files[0]} ]]; then
# Apply the `basename` command with suffix-stripping to all matches
# and read the results robustly into an array.
# Note that just `names=( $(basename ...) )` would NOT work robustly.
readarray -t names < <(basename -s '.txt' "${files[@]}")
# Note: `readarray` requires Bash 4; in Bash 3.x, use the following:
# IFS=$'\n' read -r -d '' -a names < <(basename -s '.txt' "${files[@]}")
fi
# "${names[@]}" now contains an array of suffix-stripped basenames,
# or is empty, if no files matched.
printf '%s\n' "${names[@]}" # print names line by line
注意:-e
测试有一个小警告:如果有匹配项并且第一个匹配项是损坏的符号链接(symbolic link),测试将错误地断定有没有匹配项。
一个更健壮的选项是使用 shopt -s nullglob
使 shell 将不匹配的 glob 扩展为空字符串,但请注意这是一个 shell 全局选项,返回之后它恢复到以前的值,这使得该方法更加麻烦。
关于linux - basename 命令混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33170551/
出于某种原因,我的代码正在做这种奇怪的事情,其中 fileparse 只打印(字面意思)File::Basename use strict; use warnings 'all'; use
>>> os.path.basename("../dir/") '' $ basename ../dir/ dir documentation os.path.basename(path) Retur
我确定我遗漏了什么,但我无法弄清楚。鉴于: $ find -type f ./hello.txt ./wow.txt ./yay.txt 为什么接下来的两个命令呈现不同的结果? $ find -typ
非常感谢您的帮助。 标题说明一切:使用有什么区别: echo `basename $HOME` 和 echo $(basename $HOME) 请注意,我知道 basename 命令的作用,两种语法
我有一个文件数组,我试图仅获取文件的基本名称,而不包含长扩展名。下面是数组的示例: [ '/public/uploads/contentitems/.DS_Store', '/public/u
我正在编写一个需要接收文件名的 bash 脚本,它可能类似于: “文件名 - Copy.txt” 当我尝试获取此文件的文件名时,basename 似乎将破折号作为操作数拾取并失败。 basename:
FOLDERS=$( basename "$(find "${LOG_DIR}" ! -path "${LOG_DIR}" -type d )") /storage/archive/fakeagent
我写了一个 shell 脚本来把我这边的很多 JPG 转换成 pdf #!/bin/bash set -xv for i in `ls -v *.JPG`; do i=$(basename "$i
给定以下命令: $(basename "/this-directory-does-not-exist/*.txt" ".txt") 它不仅输出 txt 文件,还输出其他文件。另一方面,如果
我需要一个作为 bash 脚本参数的文件的基本名称。基本名称应该去掉其文件扩展名。假设 $1 = "/somefolder/andanotherfolder/myfile.txt",所需的输出将是 "
我们有: $path = "/home/httpd/html/index.php"; $file = basename($path); // $file is set to "inde
我有一个奇怪的错误,在下面的代码中,basename() 已完成删除 $file: $files = glob(DIR_APPLICATION . 'controller/module/*.php')
我有一个奇怪的错误,在下面的代码中,basename() 已完成删除 $file: $files = glob(DIR_APPLICATION . 'controller/module/*.php')
我在一个 Windows 盒子上,我正在使用 basename 从一些目录中提取文件名。显然,文件名的大小是有限制的,否则 basename 会抛出一个错误(之前我在 linux 上,我不记得有什么问
编写脚本来优化我的网络图片。名称中包含空格的文件名和目录有问题。 这是我所拥有的: read -p "Enter full path from root (/) to your site... exa
这是我的片段: __VERSION="0.0.1" __COMMAND="${basename}" __USAGE="$__COMMAND -abc args" if [ $# == 0 ] ; th
#!/bin/bash file=debian.deb test=basename $file .deb DP="blah/blah/$test/$test.php" read -p "[$DP]:
在 bash Linux 中 v1=$(basename $0) gives filename.ext v1=${v1%.*}.log replaces e
运行 os.path.basename(__file__) 有时给我的文件名是 foo.py,但有时它会给出 foo.pyc。我知道pyc是编译后的文件,编译后可能每次都使用它。我只是对为什么 bas
这是一个简单的例子来说明这个问题: print basename('léquipe.jpg'); // léquipe.jpg print basename('équipe.jpg'); // qu
我是一名优秀的程序员,十分优秀!