bash - ash 是否具有与 bash 的 'nullglob' 选项等效的选项?

标签 bash shell scripting shopt

如果 glob 模式不匹配任何文件,bash 将只返回文字模式:

bash-4.1# echo nonexistent-file-*
nonexistent-file-*
bash-4.1#

您可以通过设置 nullglob shell 选项来修改默认行为,这样如果没有匹配项,您将得到一个空字符串:

bash-4.1# shopt -s nullglob
bash-4.1# echo nonexistent-file-*

bash-4.1# 

那么 ash 中是否有等效的选项?

bash-4.1# ash
~ # echo nonexistent-file-*
nonexistent-file-*
~ # shopt -s nullglob
ash: shopt: not found
~ # 

最佳答案

对于没有 nullglob 的 shell,例如 ash 和 dash:

IFS="`printf '\n\t'`"   # Remove 'space', so filenames with spaces work well.

# Correct glob use: always use "for" loop, prefix glob, check for existence:
for file in ./* ; do        # Use "./*", NEVER bare "*"
    if [ -e "$file" ] ; then  # Make sure it isn't an empty match
        COMMAND ... "$file" ...
    fi
done

来源:Filenames and Pathnames in Shell: How to do it correctly ( cached )

关于bash - ash 是否具有与 bash 的 'nullglob' 选项等效的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839214/

相关文章:

bash - bash:如何将脚本名称分配给变量并从另一个脚本调用它

python - 使用 VB 或 python 编写 OpenOffice 表单脚本

c - 如何使用 C 编写脚本语言?

mysql - 是否可以从另一个 sql 脚本中的存储过程调用 sql 脚本?

Linux 脚本 : Reinterpret environment variable

linux - 如何通过 linux shell 检查 url 是否正常工作

python - 在python程序中获取类似git的选项完成

shell - 如何添加一个变量来计算脚本被调用的次数?

mysql - 从终端或 shell 脚本制作 neo4j 命令

linux - 如何在 Linux 中隐藏 wget 输出?