根据以下命令:
$(basename "/this-directory-does-not-exist/*.txt" ".txt")它不仅输出txt文件,还输出其他文件。另一方面,如果我将".txt“改为"gobble”,它会返回:
*.txt我对它为什么返回其他扩展类型感到困惑。
发布于 2015-10-16 12:41:22
您的问题不是来自于basename,而是由于无意中使用了shell的路径名扩展(globbing)特性,原因是缺少引用
如果使用未引用的命令替换($(...))的结果
$ 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中删除扩展名;
文件名模式*.txt没有展开为实际文件名的原因是外壳留下了不匹配未经修改的任何东西的球形模式(默认情况下)。
如果重复引用命令替换,问题就解决了:
$ echo "$(basename "/this-directory-does-not-exist/*.txt" ".txt")" # -> *但是,即使解决了这个问题,只有当glob扩展为 one 匹配文件时,命令才能正确工作,因为您使用的语法形式只支持一个文件名参数。
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测试附带一个小小的警告:如果有匹配,并且第一个匹配是一个中断的符号链接,则测试将错误地得出没有匹配的结论。
一个更健壮的选项是使用shopt -s nullglob使shell展开不匹配的全局为空字符串,但请注意这是一个shell全局选项,然后将其返回到以前的值是很好的做法,这使得这种方法更加麻烦。
发布于 2015-10-16 12:33:31
试着把引号放在整件事情周围,你正在做的事情,你的命令变成*,然后被转换成当前目录中的所有文件,这不会发生在单引号或双引号中。
https://stackoverflow.com/questions/33170551
复制相似问题