带有通配符和隐藏文件的 Bash for 循环 [英] Bash for loop with wildcards and hidden files

查看:25
本文介绍了带有通配符和隐藏文件的 Bash for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只知道一个简单的 shell 脚本并且有点困惑:

这是我的脚本:

% for f in $FILES;do echo "正在处理 $f 文件..";完毕

命令:

ls -la |grep bash

产生:

% ls -a |grep bash.bash_from_cshrc.bash_history.bash_profile.bashrc

什么时候

FILES=".bash*"

我得到与 ls -a 相同的结果(不同的格式).然而当

FILES="*bash*"

我得到这个输出:

正在处理 *bash* 文件..

这不是预期的输出,也不是我所期望的.文件名的开头不允许有通配符吗?是个 .在文件名特殊"的开头不知何故?

设置

FILES="bash*"

也不起作用.

解决方案

FILES=".bash*" 之所以有效,是因为隐藏文件名以 开头.>

FILES="bash*" 不起作用,因为隐藏文件名以 开头. 不是 b

FILES="*bash*" 不起作用,因为字符串开头的 * 通配符省略了隐藏文件.

Just witting a simple shell script and little confused:

Here is my script:

% for f in $FILES; do echo "Processing $f file.."; done

The Command:

ls -la | grep bash 

produces:

% ls -a | grep bash
.bash_from_cshrc
.bash_history
.bash_profile
.bashrc

When

FILES=".bash*"

I get the same results (different formatting) as ls -a. However when

FILES="*bash*"

I get this output:

Processing *bash* file..

This is not the expected output and not what I expect. Am I not allowed to have a wild card at the beginning of the file name? Is the . at the beginning of the file name "special" somehow?

Setting

FILES="bash*"

Also does not work.

解决方案

FILES=".bash*" works because the hidden files name begin with a .

FILES="bash*" doesn't work because the hidden files name begin with a . not a b

FILES="*bash*" doesn't work because the * wildcard at the beginning of a string omits hidden files.

这篇关于带有通配符和隐藏文件的 Bash for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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