英文:
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 "*.mp4" -or -iname "*.AVI -or -iname "*.MTS")
My script:
#!/bin/bash
destDir= '/share/Multimedia2/VideoLink/tmp'
v_end=("mp4" "AVI" "MTS");
arr_len=${#v_end[@]};
arr_len=(($arr_len--))
find -L $destDir \(( for ( i=0; i<$arr_len; i++); do ( -iname "${v_end[$i]} -or" ) done ) -iname "${v_end[$arr_len]}" \)
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="$(mktemp -d)"
trap 'rm -Rf "$tmpdir"' EXIT
cd "$tmpdir"
touch a b c A B C
# Prepare arguments for find:
inames=(a b c)
find_args=(-false)
for iname in "${inames[@]}"; do
find_args+=(-or -iname "$iname")
done
# Show and call the find command:
echo find "${find_args[@]}"
find "${find_args[@]}"
答案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='/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}" \)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论