bash - 基于文件名子字符串运行脚本(可能使用通配符)

发布于 2024-12-25 20:09:36 字数 704 浏览 0 评论 0原文

我有下面的简单脚本,它调用带有多个文件名和分隔符或一组剪切位置参数的外部脚本。我的问题:有没有办法使文件名“使用通配符动态”,因为目录将始终包含这些文件名,但两端都有额外的文本?但脚本可以进行某种匹配,以根据“包含”获取完整的文件名。

当前 /release/ext/ 目录内容:

2011storesblah.dat
hrlatest.dat
emp_new12.txt

即该目录今天包含这些文件(但下周该目录中的文件名可能会有稍微不同的前缀。 例如:

stores_newer.dat
finandhr.dat
emps.txt

脚本:

 #!/bin/bash
    FILES='/release/ext/stores.dat "|"
    /release/ext/emp.txt 1-3 4-11 15-40
    /release/ext/hr.dat "|" 2'
    for f in $FILES
    do
        echo `sh myexternalscript.sh $f`;
    done

注意:无需处理脚本中的文件与目录中的 2 个以上文件匹配的情况(它始终只匹配一个)。 而且它只能匹配脚本中指定的文件类型。 另外,我不需要递归搜索,只需要查看 /release/ext/ 目录。 我正在运行 SunOS 5.10。

I've got the below simple script that calls an external script with a number of filenames and arguments of either a delimiter or a set of cut positions. My question: is there a way to make the filename 'dynamic using wildcards' in the sense that the directory will always contain those filenames but with extra text on either end? But the script can do some sort of match up to get the full filename based on a 'contains'.

current /release/ext/ directory contents:

2011storesblah.dat
hrlatest.dat
emp_new12.txt

ie the directory contains these files today (but next week the filenames in this directory could have a slightly different prefix.
eg:

stores_newer.dat
finandhr.dat
emps.txt

Script:

 #!/bin/bash
    FILES='/release/ext/stores.dat "|"
    /release/ext/emp.txt 1-3 4-11 15-40
    /release/ext/hr.dat "|" 2'
    for f in $FILES
    do
        echo `sh myexternalscript.sh $f`;
    done

Note: there is no need to handle a scenario where the file in my script matches more than 2 files in the direc (it will always only match one).
Also it only can match the file types that are specified in the script.
Also, I don't need to search recursively, just needs to look in the /release/ext/ directory only.
I'm running SunOS 5.10.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2025-01-01 20:09:36
$FILES=`find /release/ext -name *stores*.dat`
for FILE in $FILES do
  # need to test for empty, case $FILES is empty
  test -n "$FILE" && /do/whatever/you/want
done;
$FILES=`find /release/ext -name *stores*.dat`
for FILE in $FILES do
  # need to test for empty, case $FILES is empty
  test -n "$FILE" && /do/whatever/you/want
done;
弱骨蛰伏 2025-01-01 20:09:36

目前尚不清楚 $FILES 变量中的管道字符和数字的用途。但是,您可能会发现以下内容有用:

#!/bin/bash
filespecs='*stores*.dat *hr*.dat *emp*.txt'
dir='/release/ext'

cd "$dir"
for file in $filespecs
do
    sh myexternalscript.sh "$dir/$file"
done

请注意,您的问题被标记为“bash”,并且您在 shebang 中使用“bash”,但由于某种原因,您在调用其他脚本时使用“sh”。在某些系统上,sh 符号链接到 Bash,但直接调用时它的行为与 Bash 不同。在许多系统上,sh 与 Bash 完全分开。

为了扩展 glob 并合并其他参数,您需要违反始终引用变量的 Bash 规则(这是例外之一的示例)。

filespecs='*stores*.dat | 3
*hr*.dat 4 5
*emp*.txt 6 7 8'
while read -r spec arg1 arg2 arg3 arg4
do
    sh myexternalscript.sh "$dir"/$spec "$arg1" "$arg2" "$arg3" "$arg4"
done < <(echo "$filespecs")

使用您认为需要的尽可能多的“arg”参数。 Extras 将作为空传递,但设置参数。如果参数多于接受它们的变量,则最后一个变量将包含除与其对应的余数之外的所有余数。此版本不需要 cd ,因为在添加目录之前 glob 不会展开,而在第一个版本中,glob 在添加目录之前展开。

如果您按照问题中显示的方式引用管道,则双引号将包含在参数中。按照我展示的方式,只有管道字符被传递,但它受到保护,因为变量在引用时被引用。

It is unclear what the pipe characters and numbers are for in your $FILES variable. However, here is something you might find useful:

#!/bin/bash
filespecs='*stores*.dat *hr*.dat *emp*.txt'
dir='/release/ext'

cd "$dir"
for file in $filespecs
do
    sh myexternalscript.sh "$dir/$file"
done

Note that your question is tagged "bash" and you use "bash" in your shebang, but for some reason, you use "sh" when you call your other script. On some systems, sh is symlinked to Bash, but it will behave differently than Bash when called directly. On many systems, sh is completely separate from Bash.

In order to expand the globs and incorporate other arguments, you will need to violate the Bash rule of always quoting variables (this is an example of one of the exceptions).

filespecs='*stores*.dat | 3
*hr*.dat 4 5
*emp*.txt 6 7 8'
while read -r spec arg1 arg2 arg3 arg4
do
    sh myexternalscript.sh "$dir"/$spec "$arg1" "$arg2" "$arg3" "$arg4"
done < <(echo "$filespecs")

Use as many "arg" arguments as you think you'll need. Extras will be passed as empty, but set arguments. If there are more arguments than variables to accept them, then the last variable will contain all the remainders in addition to the one that corresponds to it. This version doesn't need the cd since the glob isn't expanded until the directory has been prepended, while in the first version the glob is expanded before the directory is prepended.

If you quote the pipes in the manner shown in your question, then the double quotes will be included in the argument. In the way I show it, only the pipe character gets passed but it's protected since the variable is quoted at the time it's referenced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文