如何将 ext glob 从 sh 传递到 bash 脚本中,然后展开它

问题描述 投票:0回答:1

我有脚本

se.sh
:

#!/bin/bash

files="$1"

shopt -s nullglob
shopt -s extglob
for f in a/path/to/all/files/${files}; do
  echo "file process:${f}"
done
shopt -u nullglob
shopt -u extglob

当我运行:

./se.sh "a*.txt"
时,它将处理文件夹中的正确文件。 当我运行
./se.sh "{a,1}*.txt"
时,我希望处理以
a*.txt
1*.txt
开头的文件,但它没有按我的预期运行。

我找了好久了,看看

bash-循环文件

使用 glob 表达式作为 bash 脚本参数传递

如何将通配模式作为参数传递给 bash 中的函数

shell global-variables glob extglob
1个回答
0
投票

不是答案。我的评论的快速演示:

$ ls
config.json  example.tcl  tests.toml
$ files='{e,t}*'
$ echo "$files"
{e,t}*
$ declare -a x=( "$files" )
$ declare -p x
declare -a x=([0]="{e,t}*")
$ eval declare -a x=( "$files" )
$ declare -p x
declare -a x=([0]="example.tcl" [1]="tests.toml")

当然,您不想盲目地

eval
任何未经彻底验证的用户输入。

© www.soinside.com 2019 - 2024. All rights reserved.