我有一个文件夹中以相同名称开头的多个文件,但也有其他文件。让我们说他们从'情节'开始。我想在这样的模板中回显这些名字
"plot-abc";"plot-dcb";"plot-asd";...
其余的名字没有订单。我试过了
for file in /home/user/*;
do
echo '"'
echo ${file##*/}
echo '";'
done
但这是在开头和结尾都加上引号。而且无法消除无关的文件。
如果我们能找到解决方案,我将不胜感激。
提前致谢。
printf
允许您提供模板,该模板根据需要重复多次以处理所有参数:
#!/usr/bin/env bash
# ^^^^- important: not /bin/sh; bash is needed for array support
shopt -s nullglob ## if no files match the glob, return an empty list
files=( /home/user/plot-* ) ## store results in an array
# if that array is non-empty, then pass its contents as a list of arguments to printf
(( ${#files[@]} )) && { printf '"%s";' "${files[@]##*/}"; printf '\n'; }