我在一个文件夹中有多个以相同名称开头的文件,但也有其他文件。假设它们以“情节”开头。我想在这样的模板中呼应名字
"plot-abc";"plot-dcb";"plot-asd";...
其余名称中没有顺序。我尝试过
for file in /home/user/*;
do
echo '"'
echo ${file##*/}
echo '";'
done
但是这会将引号放在开头和结尾。而且无法消除不相关的文件。
如果能找到解决方案,我将不胜感激。
谢谢。
答案 0 :(得分:2)
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'; }