【问题标题】:how can I solve this problem with while in bash?在 bash 中如何解决这个问题?
【发布时间】:2020-09-27 21:47:44
【问题描述】:

我有一个包含 .jpg 文件的 images 目录,我正在执行以下代码:

for arch in $(ls $1 *.jpg);

成为$1 目录。程序报错:

ls: Cannot access '*.jpg': File or directory does not exist

我做错了什么?提前致谢。

【问题讨论】:

标签: bash while-loop glob ls


【解决方案1】:

使用/ 将目录名称连接到内容的通配符。

也没有必要使用ls

for arch in "$1"/*.jpg

【讨论】:

    【解决方案2】:

    我会使用以下方法:

    for arch in "$1"/*.jpg; do
       [ -f "$arch" ] || continue
       file="${arch##*/}"
       # do your stuff here with "$file" which is the basename
    done
    

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 1970-01-01
      • 2016-01-24
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多