在特定模板中回显文件名
Echo files names within a specific template
我在一个文件夹中有多个以相同名称开头的文件,但还有其他文件。假设它们以 'plot' 开头。我想在这样的模板中呼应名称
"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'; }
我在一个文件夹中有多个以相同名称开头的文件,但还有其他文件。假设它们以 'plot' 开头。我想在这样的模板中呼应名称
"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'; }