Feed input positional argument with wild cards to script array variable

I am sure I am missing something simple here, but I cannot figure it out.

I have a script that should work on an array of files, and I want to feed the file name pattern to the script as positional argument.

e.g:

Content of myscript.basic.sh:

#!/bin/bash

set -e

myHCfiles=(test.*.sh)
targetScripts=($(ls $1))

for i in ${targetScripts[@]};
do
echo "target file is: ${i}"
done

for i in ${myHCfiles[@]};
do
echo "HardCoded target file is: ${i}"
done

Run:

./myscript.basic.sh test.*.sh

Output:

target file is: test.1.sh
HardCoded target file is: test.1.sh
HardCoded target file is: test.2.sh
HardCoded target file is: test.3.sh

I have tried multiple options, with and without ls, with and without quotes and parenthesis, but I cannot make the targetScripts variable work as an array. I am looking forward to your suggestions

Edit: the file pattern is not the only argument I am feeding to the script, so I do not think I can use:

targetScripts=("$@")

as it reads all the arguments into targetScripts

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论