通过for循环遍历数组找到由它生成的参数?

huangapple go评论66阅读模式
英文:

find arguments generated by for loop through array?

问题

我写了一个bash脚本,用find命令和不同的-iname参数来搜索目录。我想通过for循环将-iname参数传递给find命令。-iname参数的字符串存储在一个数组中。我想通过循环遍历这些参数以生成-iname ${arr[$i]} -or表达式。最后,我添加一个单独的-iname ${arr[${#v_end[@]}-1]}参数,不带-or

最终的find命令应该像这样:

find -L $destDir (-iname "*.mp4" -or -iname "*.AVI" -or -iname "*.MTS")

我的脚本:

#!/bin/bash
destDir='/share/Multimedia2/VideoLink/tmp'
v_end=("mp4" "AVI" "MTS")
arr_len=${#v_end[@]}
arr_len=$(($arr_len - 1))

find -L $destDir \(( for ((i=0; i<$arr_len; i++)); do -iname "${v_end[$i]}" -or; done ) -iname "${v_end[$arr_len]}" \)

这段代码不起作用。我需要做什么才能将for循环的结果传递给find命令?

英文:

I write a bash script to search through directories using the find command with different -iname arguments.
I want to pass -iname arguments to the find command by using a for loop. The strings for the -iname arguments are stored in an array.
I want to loop through the arguments to generate -iname ${arr[$i]} -or expressions.
Finally I add a single -iname ${arr[${#v_end[@]}-1]} argument without -or

The final find command should be like

find -L $destDir (-iname &quot;*.mp4&quot; -or -iname &quot;*.AVI -or -iname &quot;*.MTS&quot;)

My script:

#!/bin/bash
destDir= &#39;/share/Multimedia2/VideoLink/tmp&#39;
v_end=(&quot;mp4&quot; &quot;AVI&quot; &quot;MTS&quot;);
arr_len=${#v_end[@]};
arr_len=(($arr_len--))

find -L $destDir \(( for ( i=0; i&lt;$arr_len; i++); do ( -iname &quot;${v_end[$i]} -or&quot; ) done ) -iname &quot;${v_end[$arr_len]}&quot; \)  

This doesn't work. What do I have to do to pass the for loop results to the find command?

答案1

得分: 2

以下是翻译好的部分:

你在`find`命令行上的内容一点也不像Bash。

我不建议一次性扩展数组,主要是因为脚本应该支持文件名中的空格以及其他一些(有些)常见的(有些)特殊字符,这在扩展数组并与其他标记交错时很难实现。

可以将命令行准备为另一个数组并扩展它:

希望这有帮助。如果你有任何其他问题,可以继续提问。

英文:

What you have on the find command line doesn’t look like Bash at all.

I wouldn’t recommend expanding the array in one go, mainly because the script should support spaces and a few other (somewhat) common (somewhat) special characters in file names, which is hard to do while expanding an array and interleaving it with other tokens.

One can e.g. prepare the command line as another array and expand that one:

#!/bin/bash
set -euo pipefail

# Prepare some files to search for:
tmpdir=&quot;$(mktemp -d)&quot;
trap &#39;rm -Rf &quot;$tmpdir&quot;&#39; EXIT
cd &quot;$tmpdir&quot;
touch a b c A B C

# Prepare arguments for find:
inames=(a b c)
find_args=(-false)
for iname in &quot;${inames[@]}&quot;; do
  find_args+=(-or -iname &quot;$iname&quot;)
done

# Show and call the find command:
echo find &quot;${find_args[@]}&quot;
find &quot;${find_args[@]}&quot;

答案2

得分: 0

Sure, here is the translated code portion:

#!/bin/bash

destDir='/share/Multimedia2/VideoLink/tmp'
v_end=("mp4" "AVI" "MTS")

for ext in "${v_end[@]}"; do
    args+=('-o' '-iname' "*.$ext")
done

find -L "$destDir" \( "${args[@]:1}" \)

Is there anything else you'd like to translate?

英文:

One possibility might be:

#!/bin/bash

destDir=&#39;/share/Multimedia2/VideoLink/tmp&#39;
v_end=(&quot;mp4&quot; &quot;AVI&quot; &quot;MTS&quot;)

for ext in &quot;${v_end[@]}&quot;; do
    args+=(&#39;-o&#39; &#39;-iname&#39; &quot;*.$ext&quot;)
done

find -L &quot;$destDir&quot; \( &quot;${args[@]:1}&quot; \)

huangapple
  • 本文由 发表于 2023年4月13日 19:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定