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

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

问题描述

我正在编写一个必须接收这些参数的 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 和 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 在你的脚本开始之前就扩展了命令行中的任何通配符.当您在 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天全站免登陆