【问题标题】:Looping through files with bashscript not working使用 bashscript 循环文件不起作用
【发布时间】:2019-07-25 18:13:27
【问题描述】:

我有这个有效的代码:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

for entry in "$DIR"/*
do
  echo "$entry"
done

但是当我尝试这样做时,

for entry in "$DIR/Images"/*
do
  echo "$entry"
done

然后我的脚本尝试访问名为 $DIR/Images/* 的文件

当我这样做试图解决问题时:

for entry in "$(DIR/Images)"/*
do
  echo "$entry"
done

脚本打印出我的 mac 根目录中的所有文件。如何让我的脚本循环遍历 $DIR/Images/ 中的所有文件?

当然假设存在图像

【问题讨论】:

  • for entry in "$DIR"/Images/* 怎么样?
  • 成功了,谢谢!此外,在我的脚本中,我正在运行另一个脚本。你知道我会如何暂停我当前的脚本并等到另一个脚本完成运行吗?
  • 听起来是个很棒的新问题。

标签: bash command-line terminal


【解决方案1】:
for entry in "$DIR"/Images/*

根据您的评论有效。

我不相信 "$DIR/Images"/*"$DIR"/Images/* 之间有区别。

for entry in "$DIR/Images/*"

将不起作用,因为星号未在引号中展开,并且如果 $DIR 包含空格,$DIR/Images/* 将不起作用。

然而,

mkdir "folder with spaces"
a="folder with spaces"
mkdir "$a"/bc
touch "$a"/bc/def.txt
echo "$a"/bc/*
  >> folder with spaces/bc/def.txt
echo "$a/bc"/*
  >> folder with spaces/bc/def.txt

也就是说,两种方式都可以在我的机器上运行。

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多