开发者

read the contents of a directory using shell script

开发者 https://www.devze.com 2022-12-22 15:21 出处:网络
I\'m trying to ge开发者_开发百科t the contents of a directory using shell script. My script is:

I'm trying to ge开发者_开发百科t the contents of a directory using shell script.

My script is:

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

However, my current directory contains many files with whitespaces in their names. In that case, this script fails.

What is the correct way to loop over the contents of a directory in shell scripting?

PS: I use bash.


for entry in *
do
  echo "$entry"
done


don't parse directory contents using ls in a for loop. you will encounter white space problems. use shell expansion instead

   for file in *
    do
      if [ -f "$file" ];then
       echo "$file"
      fi
    done
0

精彩评论

暂无评论...
验证码 换一张
取 消