在扩展到匹配文件之前如何访问原义通配符? [英] How to access literal wildcard argument before it's expanded to matching files?

查看:87
本文介绍了在扩展到匹配文件之前如何访问原义通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个必须接收以下参数的bash脚本:

I'm writing a bash script that must receive these arguments:

  1. 文件名(包含一组规则的文件)
  2. 文件名列表(要处理的文件,可以使用通配符)
  3. 目标文件夹(将在其中存储文件的处理后的版本)

理论上有3个参数,但实际上第二个参数会扩展,因此,如果通配符匹配多个文件,则参数的实际数量会发生变化:

In theory there are 3 parameters but in reality the second argument expands, so the real number of argument varies if the wildcard matches more than one file:

当我打电话

./myscript  file.conf *.data dest_foder

*.data扩展为与通配符匹配的文件数量,因此等效于:

*.data expands into as many files match the wildcard so it's equivalent to:

myscript file.conf this.data that.data so.data dest_foder

在处理之前,我需要确认最后一个参数是文件夹而不是文件.

如果我省略目标文件夹,例如:

If I ommit the destination folder, like:

myscript file.conf *.data

通过验证?# -ge 3的原因是通配符返回多个项,并且如果与通配符匹配的文件夹恰好存在(如this_is_a_folder.data),则扩展如下所示:

The validation ?# -ge 3 passes because the wildcard returns more than one item, and if a folder that matches the wildcard happens to exist, like this_is_a_folder.data, then the expansion is like this:

myscript file.conf this.data that.data so.data this_is_a_folder.data

该脚本将运行,但会以非预期的方式...它将处理this.data, that.data and so.data并将已处理的文件版本放在文件夹this_is_a_folder.data中.

The script will work but in a non-expected way... it will process this.data, that.data and so.data and put the processed versions of the files in the folder this_is_a_folder.data.

  • 如何避免这种情况?
  • 如何验证目标文件夹是作为参数而不是第二个参数中通配符扩展的乘积显式传递的?

在将文字"* .data"参数扩展为匹配的文件名之前,我无法找到一种方法.

I cannot find a way to read the literal "*.data" argument before it's expanded into matching filenames.

我知道如何阅读最后一个论点.

I know how to read the last argument.

  • 但是我怎么知道最后一个参数是明确传递的还是产品是通配符扩展的?

推荐答案

您无法执行此操作,因为Shell甚至在脚本启动之前都会在命令行中扩展任何通配符.在外壳程序中输入./myscript file.conf *.data dest_foder时,实际上这只是./myscript file.conf this.data that.data so.data dest_foder的简写,而不是其他命令.

You can't do this, since the shell expands any wildcards in the command line before your script even starts. When you enter ./myscript file.conf *.data dest_foder in the shell, this is effectively just a shorthand for ./myscript file.conf this.data that.data so.data dest_foder, not a different command.

如果需要将通配符作为实际参数传递给命令,则需要引用或转义该通配符. ./myscript file.conf '*.data' dest_foder./myscript file.conf \*.data dest_foder之类的东西.或者,将最后一个参数设为强制性,或将其转换为选项(-d dest_folder),这样预扩展的文件列表就不成问题.

If you need the wildcard passed into the command as an actual argument, you need to quote or escape it. Something like ./myscript file.conf '*.data' dest_foder or ./myscript file.conf \*.data dest_foder. Alternately, make the last argument mandatory, or turn it into an option (-d dest_folder) so the preexpanded file list isn't a problem.

换一种说法:如果您不必在grep模式中引用/转义通配符,那将非常方便;但您必须这样做,因为grep命令无法以未扩展的形式获取其参数.而且,如果grep的作者无法弄清楚如何使他们的命令更方便,那么您将不可能做到这一点...

Put it another way: it'd be very convenient if you didn't have to quote/escape wildcards in your grep patterns; but you have to, because there's no way for the grep command to get at its arguments in unexpanded form. And if the authors of grep couldn't figure out how to make their command more convenient, there's no way you're going to be able to do it...

这篇关于在扩展到匹配文件之前如何访问原义通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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