如何在字符串数组中只获取.json文件名来迭代文件名
问题:我有12个.json文件,位于路径/side/containers_automation/sc/2021-05/此路径也可以具有不同的文件扩展名。
find/side/containers_automation/sc/2021-05-type f-name"*。json";
/side/containers_automation/sc/2021-05/ci-userstory-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-spe.json
/side/containers_automation/sc/2021-05/ci-e2e-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-inbound.json
/side/containers_automation/sc/2021-05/cucumber-ci-e2e-ta-dm.json
/side/containers_automation/sc/2021-05/cucumber-ci-e2e-tlm.json
/side/containers_automation/sc/2021-05/bvt-dm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-outbound-execution.json
/side/containers_automation/sc/2021-05/ci-e2e-dm.json
/side/containers_automation/sc/2021-05/cucumber-ci-userstory-tlm.json
/side/containers_automation/sc/2021-05/ci-userstory-dm-outbound.json
/side/containers_automation/sc/2021-05/uat-dm.json
只想复制.json类型的文件名,并在这些文件名之前附加一个关键字(例如:automation(。例如:自动化ci用户界面tlm自动化-ci-e2e-ta-dm自动化uat-dm等并将这些名称存储在字符串数组中,以便我可以对此进行迭代。
我是shell脚本的新手。在这方面需要你的帮助。非常感谢。我的想法->
通过命令获取文件名
(find/side/containers_automation/sc/2021-05-type f-name"*.json">conf_search(
微调初始路径
/side/containers_automation/sc/2021-05(和文件格式(.json(
/side/containers_automation/sc/2021-05/uat-dm.json3.只需获取uat、ci userstory tlm、ci usersory tlm,将其放入字符串数组中4.反复讨论我知道这是一个复杂而漫长的解决方案。
您只想将数组中特定目录中的所有JSON文件减去路径并将automation-
添加到名称的开头?
易于使用bash参数替换来操作以完整文件名开头的数组元素:
# Populate the array with the full paths to the files
filenames=( /side/containers_automation/sc/2021-05/*.json )
# Remove the path from each element
filenames=( "${filenames[@]##*/}" )
# Remove the .json extension from each element
filenames=( "${filenames[@]/%.json}" )
# Prepend automation- to each element
filenames=( "${filenames[@]/#/automation-}" )
# And show the final results for the sake of the example
declare -p filenames
或者,使用显式循环一次向数组添加一个文件,并使用basename(1)
一次性剥离路径和扩展名,而不是两步:
declare -a filenames
for file in /side/containers_automation/sc/2021-05/*.json; do
filenames+=( "automation-$(basename "$file" .json)" )
done
declare -p filenames