如何获得的文件列表中的shell脚本目录? [英] How to get the list of files in a directory in a shell script?

查看:210
本文介绍了如何获得的文件列表中的shell脚本目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用shell脚本得到一个目录的内容。

我的脚本是:

 在`LS条目$ search_dir`;做
    回声$入口
DONE

其中, $ SEARCH_DIR 是相对路径。然而, $ SEARCH_DIR 包含在其名称中的空格许多文件。在这种情况下,预期该脚本无法运行。

我知道我可以使用在* 项,但将只为我的当前目录工作。

我知道我可以切换到该目录,使用在*条目再改回来,但我的特殊情况prevents我做的。

我有两个相对路径 $ SEARCH_DIR $ WORK_DIR ,我必须在两者同时进行工作,阅读他们创建/在等他们删除文件。

所以,现在我该怎么办?

PS:我使用bash


解决方案

在$ SEARCH_DIR条目/ *

  回声$条目
DONE

I'm trying to get the contents of a directory using shell script.

My script is:

for entry in `ls $search_dir`; do
    echo $entry
done

where $search_dir is a relative path. However, $search_dir contains many files with whitespaces in their names. In that case, this script does not run as expected.

I know I could use for entry in *, but that would only work for my current directory.

I know I can change to that directory, use for entry in * then change back, but my particular situation prevents me from doing that.

I have two relative paths $search_dir and $work_dir, and I have to work on both simultaneously, reading them creating/deleting files in them etc.

So what do I do now?

PS: I use bash.

解决方案

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

这篇关于如何获得的文件列表中的shell脚本目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆