Shell 脚本列出目录中的所有文件

发布于 2025-01-11 05:00:52 字数 275 浏览 0 评论 0原文

我正在使用以下代码:

#!/bin/bash
for f in $1 ; do
        echo $f
done 

The aim is to list down all the files in the directory that is passed as an argument to this script. But it's not printing anything. Not sure what could be wrong with this.

I am using the following code :

#!/bin/bash
for f in $1 ; do
        echo $f
done 

The aim is to list down all the files in the directory that is passed as an argument to this script. But it's not printing anything. Not sure what could be wrong with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2025-01-18 05:00:52

试试这个 Shellcheck - 干净的纯 Bash 代码,以实现 评论

#! /bin/bash -p

# List all subdirectories of the directory given in the first positional
# parameter.  Include subdirectories whose names begin with dot.  Exclude
# symlinks to directories.

shopt -s dotglob
shopt -s nullglob
for d in "$1"/*/; do
    dir=${d%/}                  # Remove trailing slash
    [[ -L $dir ]] && continue   # Skip symlinks
    printf '%s\n' "$dir"
done
  • shopt -s dotglob< /code> 使 shell glob 模式匹配以点 (.) 开头的名称。 (find 默认情况下会执行此操作。)
  • shopt -s nullglob 会导致 shell glob 模式在没有匹配项时扩展为空,因此循环 glob 模式是安全的。
  • glob 模式上的尾部斜杠 ("$1"/*/) 导致仅匹配目录(包括目录的符号链接)。删除它 (dir=${d%/}) 部分是为了简洁,但主要是为了使符号链接测试 ([[ -L $dir ]]) 能够工作。
  • 请参阅 为什么 printf 比 echo 更好? 的已接受且优秀的答案,以解释我为什么使用 < code>printf 而不是 echo 来打印子目录路径。

Try this Shellcheck-clean pure Bash code for the "further plan" mentioned in a comment:

#! /bin/bash -p

# List all subdirectories of the directory given in the first positional
# parameter.  Include subdirectories whose names begin with dot.  Exclude
# symlinks to directories.

shopt -s dotglob
shopt -s nullglob
for d in "$1"/*/; do
    dir=${d%/}                  # Remove trailing slash
    [[ -L $dir ]] && continue   # Skip symlinks
    printf '%s\n' "$dir"
done
  • shopt -s dotglob causes shell glob patterns to match names that begin with a dot (.). (find does this by default.)
  • shopt -s nullglob causes shell glob patterns to expand to nothing when nothing matches, so looping over glob patterns is safe.
  • The trailing slash on the glob pattern ("$1"/*/) causes only directories (including symlinks to directories) to be matched. It's removed (dir=${d%/}) partly for cleanliness but mostly to enable the test for a symlink ([[ -L $dir ]]) to work.
  • See the accepted, and excellent, answer to Why is printf better than echo? for an explanation of why I used printf instead of echo to print the subdirectory paths.
逐鹿 2025-01-18 05:00:52

如果您只需要列出文件而不是目录。 (这部分我不清楚。) find 是你的朋友。

find $1 -depth 1 -type file

返回:

./output.tf
./locals.tf
./main.tf
./.tflint.hcl
./versions.tf
./.pre-commit-config.yaml
./makefile
./.terraformignore
./jenkins.tf
./devops.tf
./README.md
./.gitignore
./variables.tf
./Jenkinsfile
./accounts.tf
./.terraform.lock.hcl

此外,请运行man find

If you only need to list files not directories. (this part is unclear to me.) find is your friend.

find $1 -depth 1 -type file

Returns:

./output.tf
./locals.tf
./main.tf
./.tflint.hcl
./versions.tf
./.pre-commit-config.yaml
./makefile
./.terraformignore
./jenkins.tf
./devops.tf
./README.md
./.gitignore
./variables.tf
./Jenkinsfile
./accounts.tf
./.terraform.lock.hcl

Furthermore, please run man find.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文