如何在 bash 中同时循环多个通配符?

tjg*_*ant 5 bash shell for-loop wildcard

在 bash 中,我编写了一个函数,该函数使用通配符循环遍历具有多个扩展名的目录中的文件,并仅复制这些特定文件(如果存在)。代码片段如下所示:

set -e

pushd ../../../../$3/code/project/common/ > /dev/null
mkdir -p ../$2
shopt -s nullglob

for file in *.gyp; do   cp $file ../$2/; done
for file in *.gypi; do  cp $file ../$2/; done
for file in *.sh; do    cp $file ../$2/; done
for file in *.patch; do cp $file ../$2/; done

popd > /dev/null
Run Code Online (Sandbox Code Playgroud)

我更愿意编写一条for file in x语句,这样我就不必为我想要复制的每个扩展名复制并粘贴这一行。

如何将这四个for语句重写为一个for语句?如果可能的话,我想将*.ext通配符格式保留在其中。

Joe*_*Joe 7

你应该能够做到

for file in *.gyp *.gypi *.sh *.patch; do   cp $file ../$2/; done
Run Code Online (Sandbox Code Playgroud)